Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB - db.save()- Type Error- db.collection.save is not a function

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>

like image 791
Muhammed Sibly B Avatar asked Oct 19 '25 11:10

Muhammed Sibly B


1 Answers

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

db.myCol.replaceOne({"_id": ObjectId("62dd738a976a6f6126179769")},{"name":"New Element"})
like image 173
Muhammed Sibly B Avatar answered Oct 21 '25 02:10

Muhammed Sibly B