[英]How to query a single embedded document in an array in MongoDB?
我正在尝试在 MongoDB 的数组中查询单个嵌入文档。 我不知道我做错了什么。 以编程方式,我将查询此文档并将新的嵌入文档插入到当前为空的trips
数组中。
{
"_id" : ObjectId("564b3300953d9d51429163c3"),
"agency_key" : "DDOT",
"routes" : [
{
"route_id" : "6165",
"route_type" : "3",
"trips" : [ ]
},
{
"route_id" : "6170",
"route_type" : "3",
"trips" : [ ]
},
...
]
}
以下查询 - 我在 mongo shell 中运行 - 返回空:
db.tm_routes.find( { routes : {$elemMatch: { route_id:6165 } } } ).pretty();
db.tm_routes.find( { routes : {$elemMatch: { route_id:6165,route_type:3 } } } ).pretty();
db.tm_routes.find({'routes.route_id':6165}).pretty()
还有db.tm_routes.find({'routes.route_id':6165}).count()
是0
。
以下查询返回数组中的每个文档
db.tm_routes.find({'routes.route_id':'6165'}).pretty();
{
"_id" : ObjectId("564b3300953d9d51429163c3"),
"agency_key" : "DDOT",
"routes" : [
{
"route_id" : "6165",
"route_type" : "3",
"trips" : [ ]
},
{
"route_id" : "6170",
"route_type" : "3",
"trips" : [ ]
},
...
]}
但是db.tm_routes.find({'routes.route_id':'6165'}).count()
返回 1。
最后,这是我在 Node.JS 中首先插入数据的方式:
async.waterfall([
...
//RETRIEVE ALL ROUTEIDS FOR EVERY AGENCY
function(agencyKeys, callback) {
var routeIds = [];
var routesArr = [];
var routes = db.collection('routes');
//CALL GETROUTES FUNCTION FOR EVERY AGENCY
async.map(agencyKeys, getRoutes, function(err, results){
if (err) throw err;
else {
callback(null, results);
}
});
//GET ROUTE IDS
function getRoutes(agencyKey, callback){
var cursor = routes.find({agency_key:agencyKey});
cursor.toArray(function(err, docs){
if(err) throw err;
for(i in docs){
routeIds.push(docs[i].route_id);
var routeObj = {
route_id:docs[i].route_id,
route_type:docs[i].route_type,
trips:[]
};
routesArr.push(routeObj);
/* I TRIED 3 DIFFERENT WAYS TO PUSH DATA
//1->
collection.update({agency_key:agencyKey}, {$push:{"routes":{
'route_id':docs[i].route_id,
'route_type':docs[i].route_type,
'trips':[]
}}});
//2->
collection.update({agency_key:agencyKey}, {$push:{"routes":routeObj}});
*/
}
// 3->
collection.update({agency_key:agencyKey}, {$push:{routes:{$each:routesArr}}});
callback(null, routeIds);
});
};
},
...
var collection = newCollection(db, 'tm_routes',[]);
function newCollection(db, name, options){
var collection = db.collection(name);
if (collection){
collection.drop();
}
db.createCollection(name, options);
return db.collection(name);
}
注意:我没有使用猫鼬,如果可能也不想使用。
梅利斯,
我明白你在要求什么,你需要的是帮助理解事物是如何存储在 mongodb 中的。 需要了解的事情:
document
是 MongoDB 的基本数据单位,可以粗略地比作关系数据库中的row
。collection
视为具有动态模式的表因此documents
存储在collections
。每个文档都有一个特殊的_id
,它在集合中是唯一的。 您在上面以以下格式向我们展示的是One document
。
{
"_id" : ObjectId("564b3300953d9d51429163c3"),
"agency_key" : "DDOT",
"routes" : [
{
"route_id" : "6165",
"route_type" : "3",
"trips" : [ ]
},
{
"route_id" : "6170",
"route_type" : "3",
"trips" : [ ]
},
...
]}
如果您在tm_routes
集合中运行query
。 find()
将返回collection
中与该查询匹配的每个document
。 因此,当您运行查询db.tm_routes.find({'routes.route_id':'6165'}).pretty();
它返回与查询匹配的整个文档。 所以这个说法是错误的:
以下查询返回数组中的每个文档
如果您需要在该文档中查找特定路由,并且仅返回该路由,具体取决于您的用途,因为它是一个数组,您可能必须使用$-Positional Operator或 聚合框架。
对于使用Mongoose 的Node 和 Mongodb用户,这是针对上述问题编写查询的方法之一:
db.tm_routes.updateOne(
{
routes: {
$elemMatch: {
route_id: 6165 (or if its in a route path then **6165** could be replaced by **req.params.routeid**
}
}
},
{
$push: {
"routes.$.trips":{
//the content you want to push into the trips array goes here
}
}
}
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.