Mongo 结果中没有数组的聚合
Mongo aggegation without an array in result
我运行在我的collection上是这样的聚合:
db.getCollection('thoughts').aggregate([
{
$lookup: {
from: "users",
localField: "author_id",
foreignField: "_id",
as: "author"
}
}
])
结果是:
{
/* my thought's body */
"author" : [
{ /* my author's body */ }
]
}
是否有可能在 "author" 字段中获得相同但没有数组的结果 - 与 object 相同?
试试这个。
db.getCollection('thoughts').aggregate([
{
$lookup: {
from: "users",
localField: "author_id",
foreignField: "_id",
as: "author"
}
},
{"$unwind": "$author"}
])
$unwind 从输入文档中解构一个数组字段,为每个元素输出一个文档。
我运行在我的collection上是这样的聚合:
db.getCollection('thoughts').aggregate([
{
$lookup: {
from: "users",
localField: "author_id",
foreignField: "_id",
as: "author"
}
}
])
结果是:
{
/* my thought's body */
"author" : [
{ /* my author's body */ }
]
}
是否有可能在 "author" 字段中获得相同但没有数组的结果 - 与 object 相同?
试试这个。
db.getCollection('thoughts').aggregate([
{
$lookup: {
from: "users",
localField: "author_id",
foreignField: "_id",
as: "author"
}
},
{"$unwind": "$author"}
])
$unwind 从输入文档中解构一个数组字段,为每个元素输出一个文档。