數(shù)據(jù)準(zhǔn)備
{
"_id" : ObjectId("59982b08b8f62e79cdc29da2"),
"name" : "lily",
"age" : 12
}
{ "_id" : ObjectId("5999ebe32af74d2adccf516a"), "name" : "t", "age" : 13 }
{
"_id" : ObjectId("599ac13091cf72aa04177629"),
"name" : "xiaowang",
"age" : 16,
"habbit" : [
"banana",
"apple",
"peach"
],
"score" : [
51,
62
]
}
{
"_id" : ObjectId("599ac13091cf72aa0417762a"),
"name" : "xiaoli",
"age" : 17,
"habbit" : [
"apple",
"peach"
],
"score" : [
73,
23
]
}
{
"_id" : ObjectId("599ac13091cf72aa0417762b"),
"name" : "xiaoming",
"age" : 16,
"habbit" : [
"banana",
"apple",
"peach",
"grape"
],
"score" : [
83,
97
]
}
{
"_id" : ObjectId("599ac13091cf72aa0417762c"),
"name" : "xiaohong",
"age" : 17,
"habbit" : [
"banana",
"grape",
"tomato"
],
"score" : [
87,
83
]
}
{
"_id" : ObjectId("599ac13091cf72aa0417762d"),
"name" : "xiaofang",
"age" : 18,
"habbit" : [
"peach",
"grape"
],
"score" : [
54,
67
]
}
{
"_id" : ObjectId("599ac13091cf72aa0417762e"),
"name" : "xiaoqiu",
"age" : 17,
"habbit" : [
"potato",
"tomato"
],
"score" : [
91,
82
]
}
{
"_id" : ObjectId("599ac13091cf72aa0417762f"),
"name" : "xiaozhang",
"age" : 19,
"habbit" : [
"peach",
"grape"
],
"score" : [
79,
83
]
}
{
"_id" : ObjectId("599ac13091cf72aa04177630"),
"name" : "xiaowang",
"age" : 16,
"habbit" : [
"banana",
"apple",
"peach"
],
"score" : [
87,
89
]
}
{
"_id" : ObjectId("599ac13091cf72aa04177631"),
"name" : "xiaoli",
"age" : 17,
"habbit" : [
"apple",
"peach"
],
"score" : [
93,
95
]
}
1.全文匹配$all
db.userlist.find({habbit:{$all:['grape','peach']}}).pretty()
#habbit包含peach和grape的
{
"_id" : ObjectId("599ac13091cf72aa0417762b"),
"name" : "xiaoming",
"age" : 16,
"habbit" : [
"banana",
"apple",
"peach",
"grape"
],
"score" : [
83,
97
]
}
{
"_id" : ObjectId("599ac13091cf72aa0417762d"),
"name" : "xiaofang",
"age" : 18,
"habbit" : [
"peach",
"grape"
],
"score" : [
54,
67
]
}
{
"_id" : ObjectId("599ac13091cf72aa0417762f"),
"name" : "xiaozhang",
"age" : 19,
"habbit" : [
"peach",
"grape"
],
"score" : [
79,
83
]
}
2.符號(hào)運(yùn)算(>=<)
下邊的文檔查詢的例子是查詢score元素:一個(gè)元素可以滿足>70,另一個(gè)元素<90,或者是一個(gè)元素可以同時(shí)滿足這兩個(gè)條件钮呀。
db.userlist.find({score:{$gt:70,$lt:99}})
3. 元素存在$exists
- 某屬性是否存在$exists
db.userlist.find({habbit:{$exists:true}})
#查詢userlist中有habbit元素的所有文檔
db.userlist.find({habbit:{$exists:false}})
#查詢userlist中沒(méi)有habbit元素的所有文檔
4. 根據(jù)元素的個(gè)數(shù)查詢
db.userlist.find({habbit:{$size:3}})
#查詢有3個(gè)habbit的人
顯示結(jié)果:
{
"_id" : ObjectId("599ac13091cf72aa04177629"),
"name" : "xiaowang",
"age" : 16,
"habbit" : [
"banana",
"apple",
"peach"
],
"score" : [
51,
62
]
}
{
"_id" : ObjectId("599ac13091cf72aa0417762c"),
"name" : "xiaohong",
"age" : 17,
"habbit" : [
"banana",
"grape",
"tomato"
],
"score" : [
87,
83
]
}
{
"_id" : ObjectId("599ac13091cf72aa04177630"),
"name" : "xiaowang",
"age" : 16,
"habbit" : [
"banana",
"apple",
"peach"
],
"score" : [
87,
89
]
}
5.數(shù)組中指定下標(biāo)
db.userlist.find({'habbit.2':'peach'})
#尋找habbit下標(biāo)2為peach的元素墓懂。
{
"_id" : ObjectId("599ac13091cf72aa04177629"),
"name" : "xiaowang",
"age" : 16,
"habbit" : [
"banana",
"apple",
"peach"
],
"score" : [
51,
62
]
}
{
"_id" : ObjectId("599ac13091cf72aa0417762b"),
"name" : "xiaoming",
"age" : 16,
"habbit" : [
"banana",
"apple",
"peach",
"grape"
],
"score" : [
83,
97
]
}
{
"_id" : ObjectId("599ac13091cf72aa04177630"),
"name" : "xiaowang",
"age" : 16,
"habbit" : [
"banana",
"apple",
"peach"
],
"score" : [
87,
89
]
}
6.元素匹配$elemMatch
數(shù)據(jù)準(zhǔn)備:
> db.inventory.insertMany( [
... { item: "journal", instock: [ { warehouse: "A", qty: 5 }, { warehouse: "C", qty: 15 } ] },
... { item: "notebook", instock: [ { warehouse: "C", qty: 5 } ] },
... { item: "paper", instock: [ { warehouse: "A", qty: 60 }, { warehouse: "B", qty: 15 } ] },
... { item: "planner", instock: [ { warehouse: "A", qty: 40 }, { warehouse: "B", qty: 5 } ] },
... { item: "postcard", instock: [ { warehouse: "B", qty: 15 }, { warehouse: "C", qty: 35 } ] }
... ]);
查詢stock中warehouse=C的元素:
db.inventory.find({'instock':{$elemMatch:{warehouse:'C'}}})
{ "_id" : ObjectId("599af38691cf72aa04177632"), "item" : "journal", "instock" : [ { "warehouse" : "A", "qty" : 5 }, { "warehouse" : "C", "qty" : 15 } ] }
{ "_id" : ObjectId("599af38691cf72aa04177633"), "item" : "notebook", "instock" : [ { "warehouse" : "C", "qty" : 5 } ] }
{ "_id" : ObjectId("599af38691cf72aa04177636"), "item" : "postcard", "instock" : [ { "warehouse" : "B", "qty" : 15 }, { "warehouse" : "C", "qty" : 35 } ] }
7.$where
db.userlist.find({$where:'this.age<17'})