查找并替换MongoDB中的NumberLong类型字段?
update()
为此使用$set运算符。首先让我们创建一个包含文档的集合。在这里,我们将一个字段设置为NumberLong-
> db.findAndReplaceDemo.insertOne({"UserId":NumberLong(101)});{
"acknowledged" : true,
"insertedId" : ObjectId("5cd2c960b64f4b851c3a13b6")
}
> db.findAndReplaceDemo.insertOne({"UserId":NumberLong(110)});
{
"acknowledged" : true,
"insertedId" : ObjectId("5cd2c966b64f4b851c3a13b7")
}
> db.findAndReplaceDemo.insertOne({"UserId":NumberLong(101)});
{
"acknowledged" : true,
"insertedId" : ObjectId("5cd2c969b64f4b851c3a13b8")
}
> db.findAndReplaceDemo.insertOne({"UserId":NumberLong(120)});
{
"acknowledged" : true,
"insertedId" : ObjectId("5cd2c96cb64f4b851c3a13b9")
}
> db.findAndReplaceDemo.insertOne({"UserId":NumberLong(130)});
{
"acknowledged" : true,
"insertedId" : ObjectId("5cd2c96eb64f4b851c3a13ba")
}
以下是在find()
方法的帮助下显示集合中所有文档的查询-
> db.findAndReplaceDemo.find().pretty();
这将产生以下输出-
{ "_id" : ObjectId("5cd2c960b64f4b851c3a13b6"), "UserId" : NumberLong(101) }{ "_id" : ObjectId("5cd2c966b64f4b851c3a13b7"), "UserId" : NumberLong(110) }
{ "_id" : ObjectId("5cd2c969b64f4b851c3a13b8"), "UserId" : NumberLong(101) }
{ "_id" : ObjectId("5cd2c96cb64f4b851c3a13b9"), "UserId" : NumberLong(120) }
{ "_id" : ObjectId("5cd2c96eb64f4b851c3a13ba"), "UserId" : NumberLong(130) }
以下是在MongoDB中查找和替换NumberLong类型字段的查询-
> db.findAndReplaceDemo.update({"UserId":NumberLong(101)}, {$set:{"UserId":NumberLong(10001)}},false,true);WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })
让我们检查上述集合的更新结果-
> db.findAndReplaceDemo.find().pretty();
这将产生以下输出-
{"_id" : ObjectId("5cd2c960b64f4b851c3a13b6"),
"UserId" : NumberLong(10001)
}
{ "_id" : ObjectId("5cd2c966b64f4b851c3a13b7"),
"UserId" : NumberLong(110)
}
{
"_id" : ObjectId("5cd2c969b64f4b851c3a13b8"),
"UserId" : NumberLong(10001)
}
{
"_id" : ObjectId("5cd2c96cb64f4b851c3a13b9"),
"UserId" : NumberLong(120)
}
{
"_id" : ObjectId("5cd2c96eb64f4b851c3a13ba"),
"UserId" : NumberLong(130)
}
以上是 查找并替换MongoDB中的NumberLong类型字段? 的全部内容, 来源链接: utcz.com/z/331594.html