Return mongodb 中没有父对象的嵌套查询
Return nested query without parent object in mongodb
当我查询
db.Books.find({}, {_id: 0, "page.src": 1,"page.w": 1,"page.h": 1}).sort({"page.number": 1}).toArray()
它return:
[
{
"page" : [
{
"src" : "http://xxx.xxx.xxx.png",
"w" : 903,
"h" : 1300
},
{
"src" : "http://xxx.xxx.xxx2.png",
"w" : 903,
"h" : 1300
}
]
}
]
我如何 return 查询没有父对象这样的:
[
{
"src" : "http://xxx.xxx.xxx.png",
"w" : 903,
"h" : 1300
},
{
"src" : "http://xxx.xxx.xxx2.png",
"w" : 903,
"h" : 1300
}
]
这个结果将作为其他函数的输入
谢谢..
编辑
在我的机器上,@chraidarn return 的代码如下:
[
[
{
"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
},
谢谢你,
使用map()
method on the cursor returned from the find()
方法return没有父属性页面的数组。在 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
}
]
>
*One more option, you can use aggregation with map*
> db.Books.aggregate([{$unwind:"$page"},{$project:{_id:0,"page":1}}]).map(function(doc){return doc.page}
)
**Result**
[
{
"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 })
当我查询
db.Books.find({}, {_id: 0, "page.src": 1,"page.w": 1,"page.h": 1}).sort({"page.number": 1}).toArray()
它return:
[
{
"page" : [
{
"src" : "http://xxx.xxx.xxx.png",
"w" : 903,
"h" : 1300
},
{
"src" : "http://xxx.xxx.xxx2.png",
"w" : 903,
"h" : 1300
}
]
}
]
我如何 return 查询没有父对象这样的:
[
{
"src" : "http://xxx.xxx.xxx.png",
"w" : 903,
"h" : 1300
},
{
"src" : "http://xxx.xxx.xxx2.png",
"w" : 903,
"h" : 1300
}
]
这个结果将作为其他函数的输入
谢谢..
编辑
在我的机器上,@chraidarn return 的代码如下:
[
[
{
"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
},
谢谢你,
使用map()
method on the cursor returned from the find()
方法return没有父属性页面的数组。在 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
}
]
>
*One more option, you can use aggregation with map*
> db.Books.aggregate([{$unwind:"$page"},{$project:{_id:0,"page":1}}]).map(function(doc){return doc.page}
)
**Result**
[
{
"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 })