php mongodb 返回指定字段,指定$slice的情况下,如何指定返回字段?

pradb>db.t1129_1.find({},{e1:1})

{ “_id” : ObjectId(“5a1e4dfb2f4fc9561273cc6f”), “e1” : [ 800, 900 ] }

{ “_id” : ObjectId(“5a1e4ec02f4fc9561273cc70”) }

{ “_id” : ObjectId(“5a1e4f722f4fc9561273cc71”) }

{ “_id” : 700 }

{ “_id” : ObjectId(“5a279b3c5d11e0a20d21b05e”), “e1” : [ 100, 200 ] }

{ “_id” : ObjectId(“5a279b425d11e0a20d21b05f”), “e1” : [ 201, 300 ] }

{ “_id” : ObjectId(“5a279b425d11e0a20d21b060”), “e1” : [ 301, 400 ] }

{ “_id” : ObjectId(“5a31d7ae3bd6e79f91c6d091”), “e1” : [ 400, 301, 199 ] }

##现在想仅仅返回数组e1中的倒数第一个元素,但是无法做到仅返回e1这个字段

pradb>db.t1129_1.find({},{e1:{$slice:-1}})

{ “_id” : ObjectId(“5a1e4dfb2f4fc9561273cc6f”), “a1” : 12, “nr1” : { “nr11” : 11, “nr12” : 200, “nr13” : 300 }, “a3” : 100, “state” : “normal”, “e1” : [ 900 ] }

{ “_id” : ObjectId(“5a1e4ec02f4fc9561273cc70”), “a1” : 22, “nr1” : { “nr11” : 2100, “nr12” : 400, “nr13” : 500 }, “a3” : 700, “state” : “normal” }

{ “_id” : ObjectId(“5a1e4f722f4fc9561273cc71”), “a1” : 18, “nr1” : { “nr11” : 20, “nr12” : 300, “nr13” : 600 }, “a3” : 200, “state” : “normal” }

{ “_id” : 700 }

{ “_id” : ObjectId(“5a279b3c5d11e0a20d21b05e”), “e1” : [ 200 ] }

{ “_id” : ObjectId(“5a279b425d11e0a20d21b05f”), “e1” : [ 300 ] }

{ “_id” : ObjectId(“5a279b425d11e0a20d21b060”), “e1” : [ 400 ] }

{ “_id” : ObjectId(“5a31d7ae3bd6e79f91c6d091”), “e1” : [ 199 ] }

##调整成这样也不行

pradb>db.t1129_1.find({},{e1:{$slice:-1},e1:1})

{ “_id” : ObjectId(“5a1e4dfb2f4fc9561273cc6f”), “e1” : [ 800, 900 ] }

{ “_id” : ObjectId(“5a1e4ec02f4fc9561273cc70”) }

{ “_id” : ObjectId(“5a1e4f722f4fc9561273cc71”) }

{ “_id” : 700 }

{ “_id” : ObjectId(“5a279b3c5d11e0a20d21b05e”), “e1” : [ 100, 200 ] }

{ “_id” : ObjectId(“5a279b425d11e0a20d21b05f”), “e1” : [ 201, 300 ] }

{ “_id” : ObjectId(“5a279b425d11e0a20d21b060”), “e1” : [ 301, 400 ] }

{ “_id” : ObjectId(“5a31d7ae3bd6e79f91c6d091”), “e1” : [ 400, 301, 199 ] }

如果在指定$slice的情况下,想仅仅返回数组所在的字段,find()命令该怎么写呢?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值