MEAN 堆栈 - 用另一个集合中的相应名称替换文档字段数组 ID
MEAN stack - replace document field array id's with corresponding name from another collection
我有两个数据格式如下的集合 -
{
"_id":ObjectId("5b336305fa012c47c837d3b9"),
"title":"Information",
"description":"<p><strong>Hello World! This is a test editor</strong></p>",
"status":"publish",
"categories":[
"5b2b02cd317c7e100d3ae773"
],
"created_date": ISODate("2018-06-27T10:12:21.294 Z"),
"image":"/articles/server/images/1530094341293.jpg",
"owner_id":"5b1510af1d1b700edc110dda"
}
{
"_id":ObjectId("5b2b02cd317c7e100d3ae773"),
"name":"Google",
"slug":"google_1529545421927",
"description":"google another test....",
"created_date": ISODate("2018-06-21T01:43:41.918 Z")
}
第一个是Articles
,第二个是Categories
合集。
我一直在尝试的是,当我用 json
数据 -
"categories":[
"5b2b02cd317c7e100d3ae773"
],
所以我可以按以下格式发送类别名称而不是 id -
"categories":[
"Google"
],
我一直在尝试使用以下代码 -
查询 MongoDB
的 forEach
db.collection('articles').find()
.forEach(function (article) {
let categories = article.categories;
categories = categories.map(id => ObjectID(id));
var doc2 = db.collection('categories').find({_id: {$in: categories}});
console.log(doc2);
res.status(200).json('success');
});
感谢您提供的任何提示或解决方案,无需编写太多查询即可获得有效的解决方案。
注:
我尝试使用 aggregate $lookup
但我无法做到这一点,因为我必须处理 array
而不是一个值。
谢谢!
您可以使用 $lookup
- 处理这些数组并不难。
db.collection('articles').aggregate([
// {_id: '...', categories: ['...', '...', ...]}
{$project: {categories: 1}},
// {_id: '...', categories: '...'}
{$unwind: '$categories'},
// {_id: '...', categories: [{_id: '...', name: '...', ...}, ...]}
{$lookup: {
from: 'categories',
localField: 'categories',
foreignField: '_id',
as: 'categories'
}},
// {_id: '...', categories: {_id: '...', name: '...', ...}}
{$unwind: '$categories'},
// {_id: '...', categories: ['...', ...]}
{$group: {
_id: '$_id',
categories: {$addToSet: '$categories.name'}
}}
])
结果:
{
"_id" : ObjectId("5b336305fa012c47c837d3b9"),
"categories" : [
"Google"
]
}
我有两个数据格式如下的集合 -
{
"_id":ObjectId("5b336305fa012c47c837d3b9"),
"title":"Information",
"description":"<p><strong>Hello World! This is a test editor</strong></p>",
"status":"publish",
"categories":[
"5b2b02cd317c7e100d3ae773"
],
"created_date": ISODate("2018-06-27T10:12:21.294 Z"),
"image":"/articles/server/images/1530094341293.jpg",
"owner_id":"5b1510af1d1b700edc110dda"
}
{
"_id":ObjectId("5b2b02cd317c7e100d3ae773"),
"name":"Google",
"slug":"google_1529545421927",
"description":"google another test....",
"created_date": ISODate("2018-06-21T01:43:41.918 Z")
}
第一个是Articles
,第二个是Categories
合集。
我一直在尝试的是,当我用 json
数据 -
"categories":[
"5b2b02cd317c7e100d3ae773"
],
所以我可以按以下格式发送类别名称而不是 id -
"categories":[
"Google"
],
我一直在尝试使用以下代码 -
查询MongoDB
的 forEach
db.collection('articles').find()
.forEach(function (article) {
let categories = article.categories;
categories = categories.map(id => ObjectID(id));
var doc2 = db.collection('categories').find({_id: {$in: categories}});
console.log(doc2);
res.status(200).json('success');
});
感谢您提供的任何提示或解决方案,无需编写太多查询即可获得有效的解决方案。
注:
我尝试使用 aggregate $lookup
但我无法做到这一点,因为我必须处理 array
而不是一个值。
谢谢!
您可以使用 $lookup
- 处理这些数组并不难。
db.collection('articles').aggregate([
// {_id: '...', categories: ['...', '...', ...]}
{$project: {categories: 1}},
// {_id: '...', categories: '...'}
{$unwind: '$categories'},
// {_id: '...', categories: [{_id: '...', name: '...', ...}, ...]}
{$lookup: {
from: 'categories',
localField: 'categories',
foreignField: '_id',
as: 'categories'
}},
// {_id: '...', categories: {_id: '...', name: '...', ...}}
{$unwind: '$categories'},
// {_id: '...', categories: ['...', ...]}
{$group: {
_id: '$_id',
categories: {$addToSet: '$categories.name'}
}}
])
结果:
{
"_id" : ObjectId("5b336305fa012c47c837d3b9"),
"categories" : [
"Google"
]
}