Arrays 返回mongodb中没有父对象的嵌套查询
当我询问Arrays 返回mongodb中没有父对象的嵌套查询,arrays,mongodb,nested,Arrays,Mongodb,Nested,当我询问 db.Books.find({}, {_id: 0, "page.src": 1,"page.w": 1,"page.h": 1}).sort({"page.number": 1}).toArray() 它返回: [ { "page" : [ { "src" : "http://xxx.xxx.xxx.png", "w" : 903, "h"
db.Books.find({}, {_id: 0, "page.src": 1,"page.w": 1,"page.h": 1}).sort({"page.number": 1}).toArray()
它返回:
[
{
"page" : [
{
"src" : "http://xxx.xxx.xxx.png",
"w" : 903,
"h" : 1300
},
{
"src" : "http://xxx.xxx.xxx2.png",
"w" : 903,
"h" : 1300
}
]
}
]
如何在没有父对象的情况下返回查询,如下所示:
[
{
"src" : "http://xxx.xxx.xxx.png",
"w" : 903,
"h" : 1300
},
{
"src" : "http://xxx.xxx.xxx2.png",
"w" : 903,
"h" : 1300
}
]
此结果将作为其他函数的输入
谢谢
编辑
在我的机器中,@chraidarn的代码返回如下:
[
[
{
"src" : "http://xxx.xxx.xxx.png",
"w" : 903,
"h" : 1300
},
{
"src" : "http://xxx.xxx.xxx2.png",
"w" : 903,
"h" : 1300
}
]
]
我的函数仍然无法正确运行。看起来很接近。有没有进一步的想法让它完美地工作?谢谢你
编辑2
以下是相关的模式(如果需要):
book: {
type: String,
},
author: {
type: String,
},
title: {
type: String,
},
date: {
type: Date,
},
page: {
type: Array,
},
'page.$': {
type: Object
},
'page.$.number': {
type: Number
},
'page.$.src': {
type: String
},
'page.$.w': {
type: Number
},
'page.$.h': {
type: Number
},
谢谢,使用从方法返回的光标上的方法返回不带父属性页的数组。要在mongo shell中演示这一点,请执行以下操作:
> var pages = db.Books.find({},
{_id: 0, "page.src": 1,"page.w": 1,"page.h": 1})
.sort({"page.number": 1})
.map(function(doc){return doc.page});
> printjson(pages[0]); /* access the desired array result by index */
[
{
"src" : "http://xxx.xxx.xxx.png",
"w" : 903,
"h" : 1300
},
{
"src" : "http://xxx.xxx.xxx2.png",
"w" : 903,
"h" : 1300
}
]
>
这可以使用这样的聚合框架来完成
db.Books.aggregate( {$project: {"src" : "$page.src", "w": "$page.w", "h": "$page.h", _id: 0}} )
无过滤器
db.Books.distinct("page")
滤器
db.Books.distinct("page", { "page": 1 })
请使用您的问题,向我们展示您的文件;也许有更好的方法。你好@chraidarn。谢谢你的提示。在我的机器中,您的代码返回:[[{“src”:“w”:903,“h”:1300},{“src”:“w”:903,“h”:1300}]]。我的函数仍然无法正确运行。看起来很接近。有没有进一步的想法让它完美地工作?谢谢。@KarinaL您应该编辑您的问题,而不是那个答案。@KarinaL我已经编辑了我的答案,您可以使用索引0访问所需的数组。@chridam非常感谢您,现在工作非常好。我不会忘记你的帮助,谢谢你。。。
db.Books.distinct("page", { "page": 1 })