简体   繁体   English

MongoDB - db.save()- 类型错误 - db.collection.save 不是 function

[英]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.当我尝试保存 function 时,它会出错。 ie. IE。 I tried db.collection.save() my db is test and my collection name is myCol我试过 db.collection.save() 我的数据库是测试,我的集合名称是 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>

Ensure that your myCol schema defines _id :确保您的myCol架构定义_id

const myCol = Schema({
  _id: Schema.Types.ObjectId,
  name: String,
  totVal: Number,
  values: [String],
  valMin: Number
});

It's done using this: Beacause- save is depricated and use replaceOne() instead of save().使用以下方法完成:因为-save 已被贬低并使用replaceOne() 而不是save()。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM