Mongodb直接获取ObjectId
Mongodb direct access to ObjectId
我有一个 "company" collection 这样的:
{
"_id" : ObjectId("58c52a26de0bdd9311000004"),
"name" : "ed",
"projects" : [ ],
"__v" : 0,
"users" : [
ObjectId("58c52a36de0bdd9311000007"),
ObjectId("58c52a54de0bdd931100000a")
]
}
我想从用户那里得到一些信息;我是否必须执行一个查询以获取 ObjectID 和另一个对每个用户的查询?
或者有什么方法可以直接从公司查询中获取此信息?
您可以使用查找聚合从用户集合中获取数据,前提是这两个集合都在同一个数据库中。
Performs a left outer join to an unsharded collection in the same
database to filter in documents from the “joined” collection for
processing. The $lookup stage does an equality match between a field
from the input documents with a field from the documents of the
“joined” collection.
基本语法:-
{
$lookup:
{
from: <collection to join>,
localField: <field from the input documents>,
foreignField: <field from the documents of the "from" collection>,
as: <output array field>
}
}
我有一个 "company" collection 这样的:
{
"_id" : ObjectId("58c52a26de0bdd9311000004"),
"name" : "ed",
"projects" : [ ],
"__v" : 0,
"users" : [
ObjectId("58c52a36de0bdd9311000007"),
ObjectId("58c52a54de0bdd931100000a")
]
}
我想从用户那里得到一些信息;我是否必须执行一个查询以获取 ObjectID 和另一个对每个用户的查询?
或者有什么方法可以直接从公司查询中获取此信息?
您可以使用查找聚合从用户集合中获取数据,前提是这两个集合都在同一个数据库中。
Performs a left outer join to an unsharded collection in the same database to filter in documents from the “joined” collection for processing. The $lookup stage does an equality match between a field from the input documents with a field from the documents of the “joined” collection.
基本语法:-
{
$lookup:
{
from: <collection to join>,
localField: <field from the input documents>,
foreignField: <field from the documents of the "from" collection>,
as: <output array field>
}
}