[译]2.MongoDB之Selector

原文地址
除了上面我们提到的6个概念,在开始一些高级topic之前,你应该好好理解查询选择器。比如你可以在find,count,update,remove 文档的时候使用。一个选择器(selector)本质上是一个JSON对象,最简单的选择器就是{},这将匹配所有的文档。如果想找到所有雌性unicorns,我们可以使用{gendar:’f’}。
在深入挖掘(delving)选择器之前,我们先插入一些数据。首先,通过 db.unicorns.remove() 移除原有的数据,然后完成插入工作。

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});

下面我们将通过这些数据逐步掌握selector。{field:value}可以用来查找field等于value的文档;{field1:value1, field2:value2}是一个“与“声明。特殊的$lt,$lte,$gt,$gte,$ne被用来表示less than,less than or equal,greater than, greater than or equal,not equal等操作符。举例来说,为了得到体重超过700磅的雌性unicorns,我们可以这样做:

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操作符可以用来对某个field是否存在进行匹配,比如:

db.unicorns.find({vampires:{$exists:false}})

结果将返回

{ "_id" : ObjectId("5936b69f52f16625c139dd91"), "name" : "Nimue", "dob" : ISODate("1999-12-20T08:15:00Z"), "loves" : [ "grape", "carrot" ], "weight" : 540, "gender" : "f" }

$in操作符用来匹配数组中包含某值的document。

db.unicorns.find({
loves: {$in:['apple','orange']}})

上面的语句将返回喜爱apple 和 orange 的unicorn。
如果我们想在不同field之间使用OR条件而不是AND条件,可以使用$or操作符,比如:

db.unicorns.find({gender: 'f',
$or: [{loves: 'apple'},
{weight: {$lt: 500}}]})

上面将返回所有喜欢spple或者体重小于500磅的unicorn。
上面两个例子,将显得更加整洁(neat)。你可能已经注意到了,loves 是一个数组。MongoDB支持数组作为第一类对象。这是一个极其方便的特性(incredibly handy feature)。一旦你使用了它,将无法离开它。更加有意思的是,基于数组的查询非常简单:

{loves: 'watermelon'}

将返回所有loves中包含watermelon的document。
可用的操作符还有很多,可以在这里找到更多的描述Query Selectors
我们已经看到如何在find命令中使用selector。这些selector同样可以使用在remove命令,count命令,update命令等。
MongoDB产生的ObjectId类型的field _id可以这样使用:

db.unicorns.find(
{_id: ObjectId("TheObjectId")})
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,072评论 19 139
  • 关于operators 官方参考文档:https://docs.mongodb.org/manual/refere...
    我看不见阅读 7,493评论 0 3
  • 转至元数据结尾创建: 董潇伟,最新修改于: 十二月 23, 2016 转至元数据起始第一章:isa和Class一....
    40c0490e5268阅读 5,879评论 0 9
  • 初恋与你的风雨兼程 季茵是个喜欢写信的人,即便在这个手机电脑横飞的世界她的包里还是经常放着一摞信纸。 那年我回北京...
    尚诚1阅读 1,715评论 0 0
  • 你知道为什么我这么喜欢台湾吗? 小时候看过很多台湾小清新的偶像剧,比如绿光森林,王子变青蛙,命中注定我爱你,薰衣草...
    MJ1994阅读 2,849评论 0 2

友情链接更多精彩内容