码迷,mamicode.com
首页 > 数据库 > 详细

MongoDB的简单使用-查询操作

时间:2018-03-11 11:50:25      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:ack   最大   pre   object   rac   media   find   author   ini   

MongoDB通过find()函数执行查询操作

1.查找集合内的全部文档:

db.media.find()

系统返回:

{ "_id" : ObjectId("5aa47c1953350e04ddbd6fa3"), "Type" : "Book", "Title" : "Definitive Guide to MongoDB 3rd ed.", "ISBN" : "978-1-4842-1183-0", "Publisher" : "Apress", "Author" : [ "Hows,David", "Plugge,Eelco", "Membrey,Peter", "Hawkins,Tim" ] }
{ "_id" : ObjectId("5aa47dd553350e04ddbd6fa4"), "Type" : "CD", "Artist" : "Nirvana", "Title" : "Nevermind", "Tracklist" : [ { "Track" : "1", "Titie" : "Smells Like Teen Spirit", "Length" : "5:02" }, { "Track" : "2", "Title" : "In Bloom", "Length" : "4:15" } ] }

显示出上篇文章我们添加的2个文档

2.查找Nirvana的所有CD:

db.media.find({Artist:"Nirvana"})

系统返回:

{ "_id" : ObjectId("5aa47dd553350e04ddbd6fa4"), "Type" : "CD", "Artist" : "Nirvana", "Title" : "Nevermind", "Tracklist" : [ { "Track" : "1", "Titie" : "Smells Like Teen Spirit", "Length" : "5:02" }, { "Track" : "2", "Title" : "In Bloom", "Length" : "4:15" } ] }

3.查找Nirvana的所有CD的标题:

db.media.find({Artist:"Nirvana"},{Title:1})

系统返回:

{ "_id" : ObjectId("5aa47dd553350e04ddbd6fa4"), "Title" : "Nevermind" }

查找Nirvana的所有CD的非标题信息

db.media.find({Artist:"Nirvana"},{Title:0})

系统返回:

{ "_id" : ObjectId("5aa47dd553350e04ddbd6fa4"), "Type" : "CD", "Artist" : "Nirvana", "Tracklist" : [ { "Track" : "1", "Titie" : "Smells Like Teen Spirit", "Length" : "5:02" }, { "Track" : "2", "Title" : "In Bloom", "Length" : "4:15" } ] }

4.查找内嵌对象文档的信息:

db.media.find({"Tracklist.Title":"In Bloom"})

系统将返回对应文档的全部信息

{ "_id" : ObjectId("5aa47dd553350e04ddbd6fa4"), "Type" : "CD", "Artist" : "Nirvana", "Title" : "Nevermind", "Tracklist" : [ { "Track" : "1", "Titie" : "Smells Like Teen Spirit", "Length" : "5:02" }, { "Track" : "2", "Title" : "In Bloom", "Length" : "4:15" } ] }

5其他相关函数

  sort()排序函数 db.media.find().sort({Title:1})      #1为升序,-1为降序

  limit()限制返回结果的最大数目 db.media.find().limit(10)      #0表示返回所有结果

  skip()忽略集合中的前几个文档 db.media.find().skip(20)      

MongoDB的简单使用-查询操作

标签:ack   最大   pre   object   rac   media   find   author   ini   

原文地址:https://www.cnblogs.com/chzhang1994/p/8543200.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!