MongoDB简单使用-查询操作3

首先添加几个文档:

dvd = ({"Type":"DVD","Title":"Matrix,The","Released":1994,"Cast":["Keanu Reeves","Carrie-Anne Moss","Laurence Fishburne","Hugo Weaving","Gloria Foster","Joe Pantoliano"]})

db.media.insertOne(dvd)

dvd = ({"Type":"DVD","Title":"Blade Runner","Released":1982})
db.media.insertOne(dvd)

dvd = ({"Type":"DVD","Title":"Toy Story 3","Released":2010})

db.media.insertOne(dvd)

1.比较符号($gt,$gte,$lt,$lte)  #(大于,大于等于,小于,小于等于)

db.media.find({Released:{$gt:2000}},{"Cast":0})

系统返回
{ "_id" : ObjectId("5aa4984153350e04ddbd6fab"), "Type" : "DVD", "Title" : "Toy Story 3", "Released" : 2010 }

db.media.find({Released:{$gte:1994,$lt:2010}},{"Cast":0})

系统返回
{ "_id" : ObjectId("5aa497c753350e04ddbd6fa9"), "Type" : "DVD", "Title" : "Matrix,The", "Released" : 1994 }

2.$in,$all,$or

db.media.find({Released:{$in:[1994,2008,2009]}},{"Cast":0}

系统返回

{ "_id" : ObjectId("5aa497c753350e04ddbd6fa9"), "Type" : "DVD", "Title" : "Matrix,The", "Released" : 1994 }

db.media.find({Released:{$all:[1994,2008,2009]}},{"Cast":0})

系统返回

(又不存在同时满足3个条件的文档,返回空)

db.media.find({$or:[{"Title":"Toy Story 3"},{"ISBN":"978-1-4842-1183-0"}]})

系统返回
{ "_id" : ObjectId("5aa490f053350e04ddbd6fa6"), "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" ] }

3.$slice

db.media.find({"Title":"Matrix,The"},{"Cast":{$slice:3}})

系统返回
{ "_id" : ObjectId("5aa497c753350e04ddbd6fa9"), "Type" : "DVD", "Title" : "Matrix,The", "Released" : 1994, "Cast" : [ "Keanu Reeves", "Carrie-Anne Moss", "Laurence Fishburne" ] }

db.media.find({"Title":"Matrix,The"},{"Cast":{$slice:[-5,4]}})   #从最后5项中截取前4个

系统返回
{ "_id" : ObjectId("5aa497c753350e04ddbd6fa9"), "Type" : "DVD", "Title" : "Matrix,The", "Released" : 1994, "Cast" : [ "Carrie-Anne Moss", "Laurence Fishburne", "Hugo Weaving", "Gloria Foster" ] }

4.$mod

db.media.find({Released:{$mod:[3,2]}},{"Cast":0})

系统返回:

{ "_id" : ObjectId("5aa497c753350e04ddbd6fa9"), "Type" : "DVD", "Title" : "Matrix,The", "Released" : 1994 }
{ "_id" : ObjectId("5aa4981653350e04ddbd6faa"), "Type" : "DVD", "Title" : "Blade Runner", "Released" : 1982 }

5.$size

db.media.find({Tracklist:{$size:2}})

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

6.$exists

db.media.find({Author:{$exists:true}})

系统返回:
{ "_id" : ObjectId("5aa490f053350e04ddbd6fa6"), "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("5aa4915453350e04ddbd6fa8"), "Type" : "Book", "Title" : "Definitive Guide to MongoDB 3rd ed.", "ISBN" : "978-1-4842-1183-1", "Publisher" : "Apress", "Author" : [ "Hows,David", "Plugge,Eelco", "Membrey,Peter", "Hawkins,Tim" ] }

db.media.find({Author:{$exists:false}})

系统返回:
{ "_id" : ObjectId("5aa4912953350e04ddbd6fa7"), "Type" : "CD", "Artist" : "Nirvana", "Title" : "Nevermind", "Tracklist" : [ { "Track" : "1", "Title" : "Smells Like Teen Spirit", "Length" : "5:02" }, { "Track" : "2", "Title" : "In Bloom", "Length" : "4:15" } ] }
{ "_id" : ObjectId("5aa497c753350e04ddbd6fa9"), "Type" : "DVD", "Title" : "Matrix,The", "Released" : 1994, "Cast" : [ "Keanu Reeves", "Carrie-Anne Moss", "Laurence Fishburne", "Hugo Weaving", "Gloria Foster", "Joe Pantoliano" ] }
{ "_id" : ObjectId("5aa4981653350e04ddbd6faa"), "Type" : "DVD", "Title" : "Blade Runner", "Released" : 1982 }
{ "_id" : ObjectId("5aa4984153350e04ddbd6fab"), "Type" : "DVD", "Title" : "Toy Story 3", "Released" : 2010 }

7.$type

db.media.find({Tracklist:{$type:3}})   #type:3表示嵌入式对象
{ "_id" : ObjectId("5aa4912953350e04ddbd6fa7"), "Type" : "CD", "Artist" : "Nirvana", "Title" : "Nevermind", "Tracklist" : [ { "Track" : "1", "Title" : "Smells Like Teen Spirit", "Length" : "5:02" }, { "Track" : "2", "Title" : "In Bloom", "Length" : "4:15" } ] }

8.$elemMatch

db.media.find({"Tracklist.Title":"Smells Like Teen Spirit","Tracklist.Track":"1"})

系统返回:
{ "_id" : ObjectId("5aa4912953350e04ddbd6fa7"), "Type" : "CD", "Artist" : "Nirvana", "Title" : "Nevermind", "Tracklist" : [ { "Track" : "1", "Title" : "Smells Like Teen Spirit", "Length" : "5:02" }, { "Track" : "2", "Title" : "In Bloom", "Length" : "4:15" } ] }
{ "_id" : ObjectId("5aa49fe653350e04ddbd6fac"), "Type" : "CD", "Artist" : "Nirvana", "Title" : "Nirvana", "Tracklist" : [ { "Track" : "1", "Title" : "You Know You Are Right", "Length" : "3:38" }, { "Track" : "5", "Title" : "Smells Like Teen Spirit", "Length" : "5:02" } ] }

db.media.find({Tracklist:{$elemMatch:{Title:"Smells Like Teen Spirit",Track:"1"}}})

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

9.正则表达式

db.media.find({Title:/^Matrix*/i})  #i表示不区分大小写

系统返回:
{ "_id" : ObjectId("5aa497c753350e04ddbd6fa9"), "Type" : "DVD", "Title" : "Matrix,The", "Released" : 1994, "Cast" : [ "Keanu Reeves", "Carrie-Anne Moss", "Laurence Fishburne", "Hugo Weaving", "Gloria Foster", "Joe Pantoliano" ] }

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

时间: 2024-11-15 12:18:20

MongoDB简单使用-查询操作3的相关文章

MongoDB的模糊查询操作(类关系型数据库的 like 和 not like)

1.作用与语法描述 作用: 正则表达式是使用指定字符串来描述.匹配一系列符合某个句法规则的字符串.许多程序设计语言都支持利用正则表达式进行字符串操作.MongoDB 使用 $regex 操作符来设置匹配字符串的正则表达式. 语法一 { <field>: { $regex: /pattern/, $options: '<options>' } } { <field>: { $regex: 'pattern', $options: '<options>' } }

Nosql中MongoDB简单的查询练习

查询练习一部分: //1.进入my_test数据库use my_test //2.向数据库的user集合中插入一个文档 db.users.insert({username:"孙悟空"}); //3.查询user集合中的文档db.users.find(); //4.向数据库的user集合中插入一个文档db.users.insert({username:"孙娇娇"}); //5.查询数据库user集合中的文档 db.users.find(); //6.统计数据库user

在MongoDB中使用JOIN操作

SQL与NoSQL最大的不同之一就是不支持JOIN,在传统的数据库中,SQL JOIN子句允许你使用普通的字段,在两个或者是更多表中的组合表中的每行数据.例如,如果你有表books和publishers,你可以像下面这样写命令: SELECT book.title, publisher.name FROM book LEFT JOIN book.publisher_id ON publisher.id; 换句话说,book表中的publisher_id字段引用了publishers表中的id字典

MongoDB的查询操作

1. 前言 在这篇博文中,我们将学习如何查询mongoDB中的数据.当我们把数据存储在mongoDB以后,我们需要把数据查询出来.毕竟CRUD操作中,查询操作在我们系统中是我们应用比较频繁的操作.我们需要应对不同的业务需求,构造合适的查询条件去查询我们想要的数据.我们需要去学习mongoDB给我们提供了哪些查询相关的语法和功能.在这里,我们使用mongodb自带的mongo shell(mongo shell是一个javascript环境的mongodb客户端,支持js语法)来学习. 2. 准备

MongoDB源码分析——mongod数据查询操作

源码版本为MongoDB 2.6分支 Edit mongod数据查询操作 在mongod的初始化过程中说过,服务端接收到客户端消息后调用MyMessageHandler::process函数处理消息. class MyMessageHandler : public MessageHandler { public: ... virtual void process( Message& m , AbstractMessagingPort* port , LastError * le) { while

Mongodb基础用法及查询操作[转载]

插入多条测试数据> for(i=1;i<=1000;i++){... db.blog.insert({"title":i,"content":"mongodb测试文章.","name":"刘"+i});                                                      ... } db.blog.list.find().limit(10).forEach(

mongodb 各种查询操作详解

复制链接:http://www.jb51.net/article/48216.htm 一:find操作: MongoDB中使用find来进行查询,通过指定find的第一个参数可以实现全部和部分查询. 1.查询全部 空的查询文档{}会匹配集合的全部内容.如果不指定查询文档,默认就是{}. 2.部分查询 3.键的筛选 键的筛选是查询时只返回自己感兴趣的键值,通过指定find的第二个参数来实现.这样可以节省传输的数据量,又能节省客户端解码文档的时间和内存消耗. 查询时,数据库所关心的查询文档的值必须是

MongoDB各种查询操作详解

这篇文章主要介绍了MongoDB各种查询操作详解,包括比较查询.关联查询.数组查询等,需要的朋友可以参考下 一.find操作 MongoDB中使用find来进行查询,通过指定find的第一个参数可以实现全部和部分查询. 1.查询全部 空的查询文档{}会匹配集合的全部内容.如果不指定查询文档,默认就是{}. 2.部分查询 3.键的筛选 键的筛选是查询时只返回自己感兴趣的键值,通过指定find的第二个参数来实现.这样可以节省传输的数据量,又能节省客户端解码文档的时间和内存消耗. 查询时,数据库所关心

MongoDB查询操作限制返回字段的方法

这篇文章主要介绍了MongoDB查询操作限制返回字段的方法,需要的朋友可以参考下 映射(projection )声明用来限制所有查询匹配文档的返回字段.projection以文档的形式列举结果集中要包含或者排除的字段.可以指定要包含的字段(例如: {field:1})或者指定要排除的字段(例如:{field:0}).默认_id是包含在结果集合中的,要从结果集中排除_id字段,需要在 projection中指定排除_id字段({_id:0}).除了_id字段,不能在一个projection中联合使