您好,登錄后才能下訂單哦!
find除了精確查詢外,可以匹配更多的條件;
一、比較操作符
$lt代表<;
$lte代表<=;
$gt代表>;
$gte代表>=;
> db.post.find() { "_id" : ObjectId("54a530c3ff0df3732bac1681"), "id" : 2, "name" : "joe", "age" : 30, "sex" : 1, "school" : "marry" } { "_id" : ObjectId("54a530c3ff0df3732bac1680"), "id" : 1, "name" : "joe", "age" : 30, "comments" : [ "test2", "test9", "test5" ], "sex" : 1, "school" : "marry" } { "_id" : ObjectId("54a9700e1b5afd45354fd086"), "id" : 3, "test3" : 3 } { "_id" : ObjectId("54a9701c1b5afd45354fd087"), "id" : 4, "test4" : 4 } { "_id" : ObjectId("54a970281b5afd45354fd088"), "id" : 5, "test5" : 5 } { "_id" : ObjectId("54a970351b5afd45354fd089"), "id" : 6, "test6" : 6 } { "_id" : ObjectId("54a970781b5afd45354fd08a"), "id" : 7, "test7" : 7 } { "_id" : ObjectId("54a970831b5afd45354fd08b"), "id" : 8, "test8" : 8 } { "_id" : ObjectId("54a970901b5afd45354fd08c"), "id" : 9, "test9" : 9 } { "_id" : ObjectId("54a9709c1b5afd45354fd08d"), "id" : 10, "test10" : 10 } > db.post.find({"id":{"$gte":5,"$lte":7}}) { "_id" : ObjectId("54a970281b5afd45354fd088"), "id" : 5, "test5" : 5 } { "_id" : ObjectId("54a970351b5afd45354fd089"), "id" : 6, "test6" : 6 } { "_id" : ObjectId("54a970781b5afd45354fd08a"), "id" : 7, "test7" : 7 } >
$ne代表不等于:
> db.post.find({"id":{"$ne":8}}) { "_id" : ObjectId("54a530c3ff0df3732bac1681"), "id" : 2, "name" : "joe", "age" : 30, "sex" : 1, "school" : "marry" } { "_id" : ObjectId("54a530c3ff0df3732bac1680"), "id" : 1, "name" : "joe", "age" : 30, "comments" : [ "test2", "test9", "test5" ], "sex" : 1, "school" : "marry" } { "_id" : ObjectId("54a9700e1b5afd45354fd086"), "id" : 3, "test3" : 3 } { "_id" : ObjectId("54a9701c1b5afd45354fd087"), "id" : 4, "test4" : 4 } { "_id" : ObjectId("54a970281b5afd45354fd088"), "id" : 5, "test5" : 5 } { "_id" : ObjectId("54a970351b5afd45354fd089"), "id" : 6, "test6" : 6 } { "_id" : ObjectId("54a970781b5afd45354fd08a"), "id" : 7, "test7" : 7 } { "_id" : ObjectId("54a970901b5afd45354fd08c"), "id" : 9, "test9" : 9 } { "_id" : ObjectId("54a9709c1b5afd45354fd08d"), "id" : 10, "test10" : 10 } >
$in可以查詢多個鍵值:
> db.post.find({"id":{"$in":[4,2,8]}}) { "_id" : ObjectId("54a530c3ff0df3732bac1681"), "id" : 2, "name" : "joe", "age" : 30, "sex" : 1, "school" : "marry" } { "_id" : ObjectId("54a9701c1b5afd45354fd087"), "id" : 4, "test4" : 4 } { "_id" : ObjectId("54a970831b5afd45354fd08b"), "id" : 8, "test8" : 8 } >
$nin用法:
> db.post.find({"id":{"$nin":[4,2,8]}}) { "_id" : ObjectId("54a530c3ff0df3732bac1680"), "id" : 1, "name" : "joe", "age" : 30, "comments" : [ "test2", "test9", "test5" ], "sex" : 1, "school" : "marry" } { "_id" : ObjectId("54a9700e1b5afd45354fd086"), "id" : 3, "test3" : 3 } { "_id" : ObjectId("54a970281b5afd45354fd088"), "id" : 5, "test5" : 5 } { "_id" : ObjectId("54a970351b5afd45354fd089"), "id" : 6, "test6" : 6 } { "_id" : ObjectId("54a970781b5afd45354fd08a"), "id" : 7, "test7" : 7 } { "_id" : ObjectId("54a970901b5afd45354fd08c"), "id" : 9, "test9" : 9 } { "_id" : ObjectId("54a9709c1b5afd45354fd08d"), "id" : 10, "test10" : 10 } >
$or的用法:
> db.post.find({"$or":[{"sex":1},{"id":5}]}) { "_id" : ObjectId("54a530c3ff0df3732bac1681"), "id" : 2, "name" : "joe", "age" : 30, "sex" : 1, "school" : "marry" } { "_id" : ObjectId("54a530c3ff0df3732bac1680"), "id" : 1, "name" : "joe", "age" : 30, "comments" : [ "test2", "test9", "test5" ], "sex" : 1, "school" : "marry" } { "_id" : ObjectId("54a970281b5afd45354fd088"), "id" : 5, "test5" : 5 } >
$mod會將查詢的值除以第一個給定的值,若余數匹配第二個值,則匹配成功;
> db.post.find() { "_id" : ObjectId("54a530c3ff0df3732bac1681"), "id" : 2, "name" : "joe", "age" : 30, "sex" : 1, "school" : "marry" } { "_id" : ObjectId("54a530c3ff0df3732bac1680"), "id" : 1, "name" : "joe", "age" : 30, "comments" : [ "test2", "test9", "test5" ], "sex" : 1, "school" : "marry" } { "_id" : ObjectId("54a9700e1b5afd45354fd086"), "id" : 3, "test3" : 3 } { "_id" : ObjectId("54a9701c1b5afd45354fd087"), "id" : 4, "test4" : 4 } { "_id" : ObjectId("54a970281b5afd45354fd088"), "id" : 5, "test5" : 5 } { "_id" : ObjectId("54a970351b5afd45354fd089"), "id" : 6, "test6" : 6 } { "_id" : ObjectId("54a970781b5afd45354fd08a"), "id" : 7, "test7" : 7 } { "_id" : ObjectId("54a970831b5afd45354fd08b"), "id" : 8, "test8" : 8 } { "_id" : ObjectId("54a970901b5afd45354fd08c"), "id" : 9, "test9" : 9 } { "_id" : ObjectId("54a9709c1b5afd45354fd08d"), "id" : 10, "test10" : 10 } { "_id" : ObjectId("54aa8a90652d8bdfa0566d34"), "id" : 11, "test10" : 11 } > db.post.find({"id":{$mod:[5,1]}}) { "_id" : ObjectId("54a530c3ff0df3732bac1680"), "id" : 1, "name" : "joe", "age" : 30, "comments" : [ "test2", "test9", "test5" ], "sex" : 1, "school" : "marry" } { "_id" : ObjectId("54a970351b5afd45354fd089"), "id" : 6, "test6" : 6 } { "_id" : ObjectId("54aa8a90652d8bdfa0566d34"), "id" : 11, "test10" : 11 } >
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。