要从动态字段中查找的MongoDB查询
我需要从收集中查找和获取数据。这是我收集的资料要从动态字段中查找的MongoDB查询,mongodb,mapreduce,mongodb-query,Mongodb,Mapreduce,Mongodb Query,我需要从收集中查找和获取数据。这是我收集的资料 /* 1 */ { "_id" : 1, "name" : "sue", "age" : 19, "type" : 1, "points" : { "A" : { "type" : "label", "values" : "abc" }, "B" : { "mandatory" : fals
/* 1 */
{
"_id" : 1,
"name" : "sue",
"age" : 19,
"type" : 1,
"points" : {
"A" : {
"type" : "label",
"values" : "abc"
},
"B" : {
"mandatory" : false,
"type" : "text"
},
"C" : {
"mandatory" : false,
"type" : "text"
}
}
}
/* 2 */
{
"_id" : 2,
"name" : "bob",
"age" : 42,
"type" : 1,
"points" : {
"B" : {
"type" : "label",
"values" : ""
},
"C" : {
"mandatory" : false,
"type" : "text"
}
}
}
/* 3 */
{
"_id" : 3,
"name" : "ahn",
"age" : 22,
"type" : 2,
"points" : {
"A" : {
"type" : "label",
"values" : "abc"
},
"C" : {
"mandatory" : false,
"type" : "text"
}
}
}
/* 4 */
{
"_id" : 4,
"name" : "xi",
"age" : 34,
"type" : 2,
"points" : {
"A" : {
"type" : "label",
"allowedValues" : "abc"
},
"C" : {
"mandatory" : false,
"type" : "text"
}
}
}
/* 5 */
{
"_id" : 5,
"name" : "xyz",
"age" : 23,
"type" : 2,
"points" : {
"B" : {
"mandatory" : false,
"type" : "text"
},
"C" : {
"values" : "C",
"type" : "text"
}
}
}
/* 6 */
{
"_id" : 6,
"name" : "abc",
"age" : 43,
"type" : 1,
"points" : {
"A" : {
"type" : "label",
"values" : "abc"
},
"B" : {
"mandatory" : false,
"type" : "text"
}
}
}
我希望所有具有“type”=“label”
和“value”=“
的文档都来自“points”
如何使用
mongoDB
中的find()
实现此列表?有人知道这一点吗?与当前的设计一样,您需要一种机制来获取集合中所有动态键的列表,例如var dynamic_keys=[“a”、“B”、“C”]
,解析此列表以创建一个查询,如果应用于上面的查询,您的最终查询基本上应该是这样的
db.collection.find({
"$or": [
{ "points.A.type": "label", "points.A.values": "" },
{ "points.B.type": "label", "points.B.values": "" },
{ "points.C.type": "label", "points.C.values": "" }
]
})
获取动态密钥列表的第一个操作只能通过Map Reduce实现 在mongo shell中运行以下mapreduce操作将使用所有动态键作为
\u id
值填充名为temp\u collection\u keys
的单独临时集合:
mr = db.runCommand({
"mapreduce": "collection",
"map": function() {
for (var key in this.points) { emit(key, null); }
},
"reduce": function() { },
"out": "temp_collection_keys"
})
要获取所有动态键的列表,请在生成的集合上运行distinct:
db[mr.result].distinct("_id")
["A", "B", "C"]
现在给出上面的列表,您可以通过创建一个将在循环中设置其属性的对象来组装查询。通常,您的文档将具有以下结构:
var orQuery = {
"$or": [
{ "points.A.type": "label", "points.A.values": "" },
{ "points.B.type": "label", "points.B.values": "" },
{ "points.C.type": "label", "points.C.values": "" }
]
};
因此,使用上述子文档键列表,您可以使用本机JavaScript的方法动态构造上述$or数组,该方法应用于不同的数组结果:
var mr = db.runCommand({
"mapreduce": "collection",
"map": function() {
for (var key in this.points) { emit(key, null); }
},
"reduce": function() { },
"out": "temp_collection_keys"
});
var orArray = db[mr.result].distinct("_id").map(function (key){
var obj = { };
obj["points."+key+".type"] = "label";
obj["points."+key+".values"] = "";
return obj;
});
db.collection.find({ "$or": orArray });
样本输出
{
"_id" : 2,
"name" : "bob",
"age" : 42,
"type" : 1,
"points" : {
"B" : {
"type" : "label",
"values" : ""
},
"C" : {
"mandatory" : false,
"type" : "text"
}
}
}
你事先知道A、B或C是什么吗?不,那不是固定的。。可能还有更多。它的动态字段将被添加。@chridam但建议,但我是mongoDB的新成员。如果你有我可以直接应用的解决方案,那就太好了。