mongodb如何仅从查找查询中返回值列表 [英] mongodb how to return list of value only from find query

查看:154
本文介绍了mongodb如何仅从查找查询中返回值列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个集合展示位置,每个记录都有字段:placement_id,program_id,category,... 我需要找到所有具有program_id = 3或5的展示位置,并且只返回一个placement_id列表.

i have a collection placements, each record has fields: placement_id, program_id, category, ... i need to find all placements what has program_id = 3 or 5 and only return a list of placement_id.

当我尝试此命令时:

db.placements.find({program_id:{$in: [3, 5]}}, {placement_id:1, _id:0})

我有记录:

{ "placement_id" : 196 }
{ "placement_id" : 197 }
{ "placement_id" : 198 }
...

有什么方法可以返回:

[196, 197, 198...]

推荐答案

来自find()的光标将产生JSON文档,无论如何. 但是您可以提取所需的值.也许是这样的:

The cursor from find() is going to yield JSON documents, no matter what. But you can extract the values you want. Something like this perhaps :

get_placement_id = function(doc) { return doc.placement_id; }

db.placements.find({program_id:{$in: [3, 5]}}, {placement_id:1, _id:0}).map( get_placement_id )

==>

[ 196, 197, 198, ... ]

这篇关于mongodb如何仅从查找查询中返回值列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆