[英]Output values without the associated keys Mongodb
when I do the following query 当我执行以下查询时
db.mycollection.aggregate({$sample: {size: 2}}, {$project: {"_id": 1,"text": 1}})
I get the following output 我得到以下输出
"_id" : "123", "text" : "some writing"
"_id" : "456", "text" : "some more writing"
I am looking for my output to be something like this 我正在寻找我的输出是这样的
"123", "some writing"
"456", "some more writing"
Question: 题:
Is there a way to output values ( "123"
and "some writing"
) without the associated keys? 有没有一种方法可以在没有关联键的情况下输出值( "123"
和"some writing"
)? ( "_id":
and "text":
) ( "_id":
和"text":
"_id":
db.mycollection.aggregate({$sample: {size: 2}},{$project: { myArray: [ "$id", "$text" ]}})
will do the job 会做的工作
You can read more here: https://docs.mongodb.com/v3.2/reference/operator/aggregation/project/#example-project-new-array-fields 您可以在此处阅读更多信息: https : //docs.mongodb.com/v3.2/reference/operator/aggregation/project/#example-project-new-array-fields
UPDATE 1 : Updated syntax 更新1 :更新语法
I think this is with native MongoDB query syntax not possible. 我认为这与本机MongoDB查询语法不可行。 For an example on how to get this done with JavaScript have a look at the following StackOverflow post: How to return only value of a field in mongodb 有关如何使用JavaScript完成此操作的示例,请查看以下StackOverflow帖子: 如何仅返回mongodb中的字段值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.