MongoDB中在多字段设置为TRUE的情况下将值推入数组
要推送值,请将$push与update()一起使用,并将多字段设置为TRUE。让我们创建一个包含文档的集合-
> db.demo747.insertOne({"CountryName":["US","IND"]});
{
"acknowledged" : true,
"insertedId" : ObjectId("5eae6a50a930c785c834e55f")
}
> db.demo747.insertOne({"CountryName":["UK","US"]});
{
"acknowledged" : true,
"insertedId" : ObjectId("5eae6a57a930c785c834e560")
}
> db.demo747.insertOne({"CountryName":["UK","IND"]});
{
"acknowledged" : true,
"insertedId" : ObjectId("5eae6a60a930c785c834e561")
}在find()方法的帮助下显示集合中的所有文档-
> db.demo747.find();
这将产生以下输出-
{ "_id" : ObjectId("5eae6a50a930c785c834e55f"), "CountryName" : [ "US", "IND" ] }
{ "_id" : ObjectId("5eae6a57a930c785c834e560"), "CountryName" : [ "UK", "US" ] }
{ "_id" : ObjectId("5eae6a60a930c785c834e561"), "CountryName" : [ "UK", "IND" ] }以下是在update()中实现$push的正确查询-
> db.demo747.update({},{$push:{CountryName:"AUS"}},{multi:true});
WriteResult({ "nMatched" : 3, "nUpserted" : 0, "nModified" : 3 })在find()方法的帮助下显示集合中的所有文档-
> db.demo747.find();
这将产生以下输出-
{ "_id" : ObjectId("5eae6a50a930c785c834e55f"), "CountryName" : [ "US", "IND", "AUS" ] }
{ "_id" : ObjectId("5eae6a57a930c785c834e560"), "CountryName" : [ "UK", "US", "AUS" ] }
{ "_id" : ObjectId("5eae6a60a930c785c834e561"), "CountryName" : [ "UK", "IND", "AUS" ] }热门推荐
10 对患者生日祝福语简短
11 结婚祝福语简短装备
12 周岁祝福语学生文案简短
13 订婚领证祝福语简短精辟
14 导师获奖祝福语大全简短
15 新婚购房祝福语简短精辟
16 牛年祝福语简短的爱人
17 送芒果的祝福语简短
18 送给学长毕业祝福语简短