如何更新MongoDB _id集合
要更新_id,请在MongoDB中使用$set。让我们创建一个包含文档的集合-
db.demo741.insertOne({SubjectName:"MySQL"});{
"acknowledged" : true,
"insertedId" : ObjectId("5ead718657bb72a10bcf0672")
}
> db.demo741.insertOne({SubjectName:"C"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5ead718957bb72a10bcf0673")
}
> db.demo741.insertOne({SubjectName:"Java"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5ead718e57bb72a10bcf0674")
}
在find()方法的帮助下显示集合中的所有文档-
> db.demo741.find();
这将产生以下输出-
{ "_id" : ObjectId("5ead718657bb72a10bcf0672"), "SubjectName" : "MySQL" }{ "_id" : ObjectId("5ead718957bb72a10bcf0673"), "SubjectName" : "C" }
{ "_id" : ObjectId("5ead718e57bb72a10bcf0674"), "SubjectName" : "Java" }
以下是更新_id的MongoDB的查询-
>db.demo741.update({_id:ObjectId("5ead718957bb72a10bcf0673")},{$set:{SubjectName:"PL/SQL"}});WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
在find()方法的帮助下显示集合中的所有文档-
> db.demo741.find();
这将产生以下输出-
{ "_id" : ObjectId("5ead718657bb72a10bcf0672"), "SubjectName" : "MySQL" }{ "_id" : ObjectId("5ead718957bb72a10bcf0673"), "SubjectName" : "PL/SQL" }
{ "_id" : ObjectId("5ead718e57bb72a10bcf0674"), "SubjectName" : "Java" }
以上是 如何更新MongoDB _id集合 的全部内容, 来源链接: utcz.com/z/345666.html