查询返回的字段显示($,$slice,$elemMatch)
来源:互联网 发布:陈冠希道歉 知乎 编辑:程序博客网 时间:2024/05/18 14:11
1.查看容器中的所有文档db.inventory.find(){ "_id" : ObjectId("5446ebddd251b112db5bf7bc"), "item" : "ABC1", "details" : {"model" : "14Q3","manufacturer" : "XYZ Company"}, "stock" : [ { "size" : "S", "qty" : 25 },{ "size" : "M", "qty" : 50 } ], "category" : "clothing" }{ "_id" : ObjectId("5446ebfbd251b112db5bf7bd"), "item" : "ABC2", "details" : { "model" : "14Q2", "manufacturer" : "XYZ Company" }, "stock" : [ { "size" : "S", "qty" : 30 }, { "size" : "M", "qty" : 60 } ], "category" : "clothing" }{ "_id" : ObjectId("5446ec08d251b112db5bf7be"), "item" : "ABC3", "details" : { "model" : "14Q4", "manufacturer" : "XYZ Company" }, "stock" : [ { "size" : "S", "qty" : 10 }, { "size" : "M", "qty" : 20 } ], "category" : "clothing" }2.查询返回指定的字段> db.inventory.find({"item": "ABC1"},{"item":1,"_id": 0 ,"stock": 1}){ "item" : "ABC1", "stock" : [ { "size" : "S", "qty" : 25 }, { "size" : "M", "qty" : 50 } ] }3.数组字段查询返回的显示($slice)> db.inventory.find({"item": "ABC1"},{"item":1,"_id": 0 , "stock": {$slice: 1}}) //返回顺数第一个元素{ "item" : "ABC1", "stock" : [ { "size" : "S", "qty" : 25 } ] }> db.inventory.find({"item": "ABC1"},{"item":1,"_id": 0 , "stock": {$slice: 2}}){ "item" : "ABC1", "stock" : [ { "size" : "S", "qty" : 25 }, { "size" : "M", "qty" : 50 } ] }> db.inventory.find({"item": "ABC1"},{"item":1,"_id": 0 , "stock": {$slice: 3}}){ "item" : "ABC1", "stock" : [ { "size" : "S", "qty" : 25 }, { "size" : "M", "qty" : 50 } ] }> db.inventory.find({"item": "ABC1"},{"item":1,"_id": 0 , "stock": {$slice: 0}}){ "item" : "ABC1", "stock" : [ ] }> db.inventory.find({"item": "ABC1"},{"item":1,"_id": 0 , "stock": {$slice: -1}}) //返回倒数第一个元素{ "item" : "ABC1", "stock" : [ { "size" : "M", "qty" : 50 } ] }> db.inventory.find({"item": "ABC1"},{"item":1,"_id": 0 , "stock": {$slice: -2}}){ "item" : "ABC1", "stock" : [ { "size" : "S", "qty" : 25 }, { "size" : "M", "qty" : 50 } ] }> db.inventory.find({"item": "ABC1"},{"item":1,"_id": 0 , "stock": {$slice: [-1,1]}}) //$silce:[skip,limit]{ "item" : "ABC1", "stock" : [ { "size" : "M", "qty" : 50 } ] }//返回一项从倒数第一个开始返回> db.inventory.find({"item": "ABC1"},{"item":1,"_id": 0 , "stock": {$slice: [1,1]}}) //$silce:[skip,limit]{ "item" : "ABC1", "stock" : [ { "size" : "M", "qty" : 50 } ] }//return 1 items, after skipping the first 1 items of that array.> db.inventory.find({"item": "ABC1"},{"item":1,"_id": 0 , "stock": {$slice: [-2,1]}}) //$silce:[skip,limit]{ "item" : "ABC1", "stock" : [ { "size" : "S", "qty" : 25 } ] }//返回一项从倒数第二个开始返回4.$elemMatch在数组查询中的使用The $elemMatch operator limits the contents of an <array> field from the query results to contain only the first element matching the $elemMatch condition.//返回第一个满足条件的元素(如果有多个元素满足条件)> db.inventory.find({"item": "ABC1"},{"item":1,"_id": 0 , "stock": {$elemMatch: {"size": "S" } }}){ "item" : "ABC1", "stock" : [ { "size" : "S", "qty" : 25 } ] }> db.inventory.find({},{"item":1,"_id": 0 , "stock": {$elemMatch: {"size": "S" } }}){ "item" : "ABC1", "stock" : [ { "size" : "S", "qty" : 25 } ] }{ "item" : "ABC2", "stock" : [ { "size" : "S", "qty" : 30 } ] }{ "item" : "ABC3", "stock" : [ { "size" : "S", "qty" : 10 } ] }> db.inventory.find({},{"item":1,"_id": 0 , "stock": {$elemMatch: {"qty": {$gt: 20} } }}){ "item" : "ABC1", "stock" : [ { "size" : "S", "qty" : 25 } ] }{ "item" : "ABC2", "stock" : [ { "size" : "S", "qty" : 30 } ] }{ "item" : "ABC3" }> db.inventory.find({},{"item":1,"_id": 0 , "stock": {$elemMatch: {"size": "S" } }}).sort({"stock.qty": -1}){ "item" : "ABC2", "stock" : [ { "size" : "S", "qty" : 30 } ] }{ "item" : "ABC1", "stock" : [ { "size" : "S", "qty" : 25 } ] }{ "item" : "ABC3", "stock" : [ { "size" : "S", "qty" : 10 } ] }5.$在数组查询中的使用db.collection.find( { <array>: <value> ... }, { "<array>.$": 1 } )db.collection.find( { <array.field>: <value> ...}, { "<array>.$": 1 } )The <array> field being limited must appear in the query document, and the <value> can be documents that contain query operator expressions> db.inventory.find({"stock.qty": {$gt: 10}},{"stock.$": 1}) //stock必须作为查询条件的字段 并且返回的只是数组中第一个满足条件的元素{ "_id" : ObjectId("5446ebddd251b112db5bf7bc"), "stock" : [ { "size" : "S", "qty" : 25 } ] }{ "_id" : ObjectId("5446ebfbd251b112db5bf7bd"), "stock" : [ { "size" : "S", "qty" : 30 } ] }{ "_id" : ObjectId("5446ec08d251b112db5bf7be"), "stock" : [ { "size" : "M", "qty" : 20 } ] }
0 0
- 查询返回的字段显示($,$slice,$elemMatch)
- hive查询如何显示查询的字段
- mybatis查询字段为空返回另一个字段的值
- PL/SQL 查询的字段显示不全
- 记录:java执行mysql语句查询字段类型:timestamp返回页面显示会多出个 .0,自己如何处理的
- springMVC+mybatis查询数据,返回resultType=”map”时,如果数据为空的字段,则该字段省略不显示的解决方案
- 查询数据库中的表,返回字段相对应的数据类型!
- 关于ibatis 查询少量字段 返回map 的method
- Hibernate查询指定字段并返回对象的结果集
- SpringMVC查询返回集合,不能自定义字段对象的解决方法
- 通过id查询用户,但是只返回指定的字段
- MongoDB查询操作限制返回字段的方法
- 关于springmvc 返回json数据null字段的显示问题
- mongodb 内嵌文档多条件查询($elemMatch)
- 查询数据库时如何把数据按照字段名和字段值的数据返回
- 【分享】查询分析器显示了完整的字段信息
- UNPIVOT 关键字将查询的字段一行显示
- mysql 查询的字段值太长显示不全 group_concat
- C++默认参数
- oracle表空间查询维护命令大全之三(临时表空间)史上最全
- java.lang.ClassNotFoundException: org.apache.commons.pool.KeyedObjectPoolFactory解决方案
- AIX6.1/11.2.0.3数据库上关于SWAP的一个BUG
- iOS开发中@property的属性weak nonatomic strong readonly等介绍
- 查询返回的字段显示($,$slice,$elemMatch)
- TestNg依赖配置基础用法(单一方法依赖)------TestNg依赖详解(一)
- Java 信号量 Semaphore 介绍
- Android中实时获取声音频率
- spring 相关jar包下载地址(永久更新中)
- NYOJ 33 蛇形填数
- javaFilter总结
- UITableView类型为grouped时,如何设置footer的高度为0
- Java设计模式之观察者模式