skip to Main Content

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

Answers


  1. Chosen as BEST ANSWER

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

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

  2. Ensure that your myCol schema defines _id:

    const myCol = Schema({
      _id: Schema.Types.ObjectId,
      name: String,
      totVal: Number,
      values: [String],
      valMin: Number
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search