将值推入将多字段设置为 TRUE 的数组中?
要推送值,在将多字段设置为 TRUE 时使用 $push 和 update()。我们使用文档创建一个集合 −
> 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" ] }
广告
数据结构
网络
关系型数据库管理系统(RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP