在MongoDB中按特定字段搜索
首先让我们创建一个包含文档的集合-
> db.demo371.insertOne({"Name":"David","CountryName":"US"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e57f6982ae06a1609a00af2")
}
> db.demo371.insertOne({"Name":"John","CountryName":"UK"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e57f69e2ae06a1609a00af3")
}
> db.demo371.insertOne({"Name":"Bob","CountryName":"AUS"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e57f6a42ae06a1609a00af4")
}
> db.demo371.insertOne({"Name":"Mike","CountryName":"US"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e57f6ba2ae06a1609a00af5")
}在find()方法的帮助下显示集合中的所有文档-
> db.demo371.find();
这将产生以下输出-
{ "_id" : ObjectId("5e57f6982ae06a1609a00af2"), "Name" : "David", "CountryName" : "US" }
{ "_id" : ObjectId("5e57f69e2ae06a1609a00af3"), "Name" : "John", "CountryName" : "UK" }
{ "_id" : ObjectId("5e57f6a42ae06a1609a00af4"), "Name" : "Bob", "CountryName" : "AUS" }
{ "_id" : ObjectId("5e57f6ba2ae06a1609a00af5"), "Name" : "Mike", "CountryName" : "US" }以下是按特定字段搜索的查询-
> db.demo371.find({ $or: [ { Name: "David" }, { CountryName: "UK"} ] } )这将产生以下输出-
{ "_id" : ObjectId("5e57f6982ae06a1609a00af2"), "Name" : "David", "CountryName" : "US" }
{ "_id" : ObjectId("5e57f69e2ae06a1609a00af3"), "Name" : "John", "CountryName" : "UK" }热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短