原文地址
除了上面我們提到的6個(gè)概念肌厨,在開始一些高級(jí)topic之前培慌,你應(yīng)該好好理解查詢選擇器。比如你可以在find柑爸,count吵护,update,remove 文檔的時(shí)候使用表鳍。一個(gè)選擇器(selector)本質(zhì)上是一個(gè)JSON對(duì)象馅而,最簡(jiǎn)單的選擇器就是{},這將匹配所有的文檔譬圣。如果想找到所有雌性u(píng)nicorns瓮恭,我們可以使用{gendar:’f’}。
在深入挖掘(delving)選擇器之前厘熟,我們先插入一些數(shù)據(jù)屯蹦。首先,通過(guò) db.unicorns.remove() 移除原有的數(shù)據(jù)绳姨,然后完成插入工作登澜。
db.unicorns.insert({name: 'Horny',
dob: new Date(1992,2,13,7,47),
loves: ['carrot','papaya'],
weight: 600,
gender: 'm',
vampires: 63});
db.unicorns.insert({name: 'Aurora',
dob: new Date(1991, 0, 24, 13, 0),
loves: ['carrot', 'grape'],
weight: 450,
gender: 'f',
vampires: 43});
db.unicorns.insert({name: 'Unicrom',
dob: new Date(1973, 1, 9, 22, 10),
loves: ['energon', 'redbull'],
weight: 984,
gender: 'm',
vampires: 182});
db.unicorns.insert({name: 'Roooooodles',
dob: new Date(1979, 7, 18, 18, 44),
loves: ['apple'],
weight: 575,
gender: 'm',
vampires: 99});
db.unicorns.insert({name: 'Solnara',
dob: new Date(1985, 6, 4, 2, 1),
loves:['apple', 'carrot',
'chocolate'],
weight:550,
gender:'f',
vampires:80});
db.unicorns.insert({name:'Ayna',
dob: new Date(1998, 2, 7, 8, 30),
loves: ['strawberry', 'lemon'],
weight: 733,
gender: 'f',
vampires: 40});
db.unicorns.insert({name:'Kenny',
dob: new Date(1997, 6, 1, 10, 42),
loves: ['grape', 'lemon'],
weight: 690,
gender: 'm',
vampires: 39});
db.unicorns.insert({name: 'Raleigh',
dob: new Date(2005, 4, 3, 0, 57),
loves: ['apple', 'sugar'],
weight: 421,
gender: 'm',
vampires: 2});
db.unicorns.insert({name: 'Leia',
dob: new Date(2001, 9, 8, 14, 53),
loves: ['apple', 'watermelon'],
weight: 601,
gender: 'f',
vampires: 33});
db.unicorns.insert({name: 'Pilot',
dob: new Date(1997, 2, 1, 5, 3),
loves: ['apple', 'watermelon'],
weight: 650,
gender: 'm',
vampires: 54});
db.unicorns.insert({name: 'Nimue',
dob: new Date(1999, 11, 20, 16, 15),
loves: ['grape', 'carrot'],
weight: 540,
gender: 'f'});
db.unicorns.insert({name: 'Dunx',
dob: new Date(1976, 6, 18, 18, 18),
loves: ['grape', 'watermelon'],
weight: 704,
gender: 'm',
vampires: 165});
下面我們將通過(guò)這些數(shù)據(jù)逐步掌握selector。{field:value}可以用來(lái)查找field等于value的文檔飘庄;{field1:value1, field2:value2}是一個(gè)“與“聲明脑蠕。特殊的$lt,$lte,$gt,$gte,$ne被用來(lái)表示less than,less than or equal,greater than, greater than or equal,not equal等操作符。舉例來(lái)說(shuō)跪削,為了得到體重超過(guò)700磅的雌性u(píng)nicorns空郊,我們可以這樣做:
db.unicorns.find({gender: 'm',
weight: {$gt: 700}})
//or (not quite the same thing, but for
//demonstration purposes)
db.unicorns.find({gender: {$ne: 'f'},
weight: {$gte: 701}})
$exists操作符可以用來(lái)對(duì)某個(gè)field是否存在進(jìn)行匹配份招,比如:
db.unicorns.find({vampires:{$exists:false}})
結(jié)果將返回
{ "_id" : ObjectId("5936b69f52f16625c139dd91"), "name" : "Nimue", "dob" : ISODate("1999-12-20T08:15:00Z"), "loves" : [ "grape", "carrot" ], "weight" : 540, "gender" : "f" }
$in操作符用來(lái)匹配數(shù)組中包含某值的document。
db.unicorns.find({
loves: {$in:['apple','orange']}})
上面的語(yǔ)句將返回喜愛apple 和 orange 的unicorn狞甚。
如果我們想在不同field之間使用OR條件而不是AND條件锁摔,可以使用$or操作符,比如:
db.unicorns.find({gender: 'f',
$or: [{loves: 'apple'},
{weight: {$lt: 500}}]})
上面將返回所有喜歡spple或者體重小于500磅的unicorn哼审。
上面兩個(gè)例子谐腰,將顯得更加整潔(neat)。你可能已經(jīng)注意到了涩盾,loves 是一個(gè)數(shù)組十气。MongoDB支持?jǐn)?shù)組作為第一類對(duì)象。這是一個(gè)極其方便的特性(incredibly handy feature)春霍。一旦你使用了它砸西,將無(wú)法離開它。更加有意思的是址儒,基于數(shù)組的查詢非常簡(jiǎn)單:
{loves: 'watermelon'}
將返回所有l(wèi)oves中包含watermelon的document芹枷。
可用的操作符還有很多,可以在這里找到更多的描述Query Selectors莲趣。
我們已經(jīng)看到如何在find命令中使用selector鸳慈。這些selector同樣可以使用在remove命令,count命令喧伞,update命令等走芋。
MongoDB產(chǎn)生的ObjectId類型的field _id可以這樣使用:
db.unicorns.find(
{_id: ObjectId("TheObjectId")})