我是Mongodb的新手,尝试从集合中获取数据,首先我需要获取数组的所有值" 1"从下面的例子中,没有传递任何数组值,其次获取Json格式的数据,这将通过游标发送到客户端,toArray函数适用于控制台但不适用于html页面。
代码:
"_id" : ObjectId("5934f65bdab27e02aa954891"),"tracker" : [
{
"1" : [
"open",
"isnot",
"closed",
"any"
],
"2" : [
"task",
"user story",
"bug",
"support",
"feature",
"ui modification",
"enhancment",
"use case"
]
}
],
"subject" : [
{
"1" : [
"contains",
"doesnot contain",
"none",
"any"
]
}
]}
预期的Output_1(数组的值" 1"): "打开"," is not","关闭","任何"
预期的Output_2(Json格式)
"tracker" : [
{
"1" : [
"open",
"isnot",
"closed",
"any"
],
"2" : [
"task",
"user story",
"bug",
"support",
"feature",
"ui modification",
"enhancment",
"use case"
]
}],"subject" : [
{
"1" : [
"contains",
"doesnot contain",
"none",
"any"
]
}]}
答案 0 :(得分:1)
db.details.aggregate(
// Pipeline
[
// Stage 1
{
$match: {"_id" : ObjectId("5934f65bdab27e02aa954891")}
},
// Stage 2
{
$unwind: "$tracker"
},
// Stage 3
{
$project: {
'tracker.1':1
}
},
]
);
以上聚合查询在管道中按顺序执行以下聚合阶段
$ match运算符过滤器文档,仅选择文档所在的值 _id字段的等效于特定的对象ID
$ unwind运算符将数组字段拆分为单独的文档 数组的每个值。
$ project operator限制返回结果的字段。