3

When I try to make a save function, then it goes an error. ie. I tried db.collection.save() my db is test and my collection name is myCol

test> db.myCol.find()
[
  {
    _id: ObjectId("62dd5dfd976a6f6126179768"),
    name: 'J',
    totVal: 5,
    values: [ 'Hi', 'Hello', 'How' ],
    valMin: 15
  },
  {
    _id: ObjectId("62dd738a976a6f6126179769"),
    name: 'K',
    values: [ 'Hi', 'Hello', 'How' ],
    totVal: 5,
    valMin: 15
  }
]
test> db.myCol.save({"_id": ObjectId(62dd738a976a6f6126179769),"name":"New Element"})
Uncaught:
SyntaxError: Identifier directly after number. (1:33)

> 1 | db.myCol.save({"_id": ObjectId(62dd738a976a6f6126179769),"name":"New Element"})
    |                                  ^
  2 |

test>

tried this tooo

test> db.myCol.save({"_id": ObjectId("62dd738a976a6f6126179769"),"name":"New Element"})
TypeError: db.myCol.save is not a function
test>

  • 2
    `save()` is deprecated as of 4.2. Which version of MongoDB are you using? Use `db.myCol.replaceOne({"_id": ObjectId("62dd738a976a6f6126179769"),"name":"New Element"})` instead. – John Hanley Jul 26 '22 at 06:49
  • It's partially working, But not working, and error is this. `{ acknowledged: true, insertedId: null, matchedCount: 0, modifiedCount: 0, upsertedCount: 0 }` – Muhammed Sibly B Jul 27 '22 at 12:46

2 Answers2

1

It's done using this: Beacause- save is depricated and use replaceOne() instead of save().

db.myCol.replaceOne({"_id": ObjectId("62dd738a976a6f6126179769")},{"name":"New Element"})
0

Ensure that your myCol schema defines _id:

const myCol = Schema({
  _id: Schema.Types.ObjectId,
  name: String,
  totVal: Number,
  values: [String],
  valMin: Number
});
Stephen Taylor
  • 798
  • 7
  • 19