如果我的藏品是:“如果我的藏品是:”我如何找到在“2015-03-16”预订的码头名称,船名和水手名称: 我的数据库是mongodb,我想用mongo命令找到结果,没有javascript代码。
收集1:
{ "_id" : { "$oid" : "54f923b87b2b4c977e827d8d" }, "name" : "Sea View", "location" : "Petone" }
{ "_id" : { "$oid" : "54f923dd7b2b4c977e827d8e" }, "name" : "Port Nicholson", "location" : "Wellington" }
{ "_id" : { "$oid" : "54f924007b2b4c977e827d8f" }, "name" : "Evans Bay", "location" : "Wellington" }
收集2:
{ "_id" : { "$oid" : "54f92b017b2b4c977e827da8" }, "name" : "James", "sailorId" : 707, "skills" : [ "row", "sail", "motor", "fish" ], "address" : "Wellington" }
{ "_id" : { "$oid" : "54f92b2c7b2b4c977e827da9" }, "name" : "Peter", "sailorId" : 111, "skills" : [ "row", "sail", "motor" ], "address" : "Upper Hutt" }
{ "_id" : { "$oid" : "54f92bdd7b2b4c977e827daa" }, "name" : "Milan", "sailorId" : 818, "skills" : [ "row", "sail", "motor", "first aid" ], "address" : "Wellington" }
{ "_id" : { "$oid" : "54f92bff7b2b4c977e827dab" }, "name" : "Eileen", "sailorId" : 919, "skills" : [ "sail", "motor", "swim" ], "address" : "Lower Hutt" }
{ "_id" : { "$oid" : "54f92cac7b2b4c977e827dac" }, "name" : "Paul", "sailorId" : 110, "skills" : [ "row", "swim" ], "address" : "Upper Hutt" }
{ "_id" : { "$oid" : "54f92cdf7b2b4c977e827dad" }, "name" : "Charmain", "sailorId" : 999, "skills" : [ "row" ], "address" : "Upper Hutt" }
收集3:
{ "_id" : { "$oid" : "54f929fe7b2b4c977e827d9c" }, "marina" : "Sea View", "name" : "Flying Dutch", "number" : 313, "color" : "blue", "driven_by" : [ "sail" ] }
{ "_id" : { "$oid" : "54f92a037b2b4c977e827d9d" }, "marina" : "Sea View", "name" : "Blue Shark", "number" : 515, "color" : "black", "driven_by" : [ "motor" ] }
{ "_id" : { "$oid" : "54f92a0b7b2b4c977e827d9e" }, "marina" : "Sea View", "name" : "Killer Whale", "number" : 111, "color" : "back", "driven_by" : [ "row" ] }
{ "_id" : { "$oid" : "54f92a107b2b4c977e827d9f" }, "marina" : "Sea View", "name" : "Dolphin", "number" : 110, "color" : "blue", "driven_by" : [ "sail, motor" ] }
{ "_id" : { "$oid" : "54f92a177b2b4c977e827da0" }, "marina" : "Port Nicholson", "name" : "Red Cod", "number" : 616, "color" : "yellow", "driven_by" : [ "sail, motor" ] }
{ "_id" : { "$oid" : "54f92a1e7b2b4c977e827da1" }, "marina" : "Port Nicholson", "name" : "Tarakihi", "number" : 717, "color" : "red", "driven_by" : [ "row", "motor" ] }
{ "_id" : { "$oid" : "54f92a237b2b4c977e827da2" }, "marina" : "Port Nicholson", "name" : "Night Breeze", "number" : 818, "color" : "black", "driven_by" : [ "row" ] }
{ "_id" : { "$oid" : "54f92a2a7b2b4c977e827da3" }, "marina" : "Port Nicholson", "name" : "Mermaid", "number" : 919, "color" : "white", "driven_by" : [ "sail", "motor" ] }
{ "_id" : { "$oid" : "54f92a2f7b2b4c977e827da4" }, "marina" : "Port Nicholson", "name" : "Dolphin", "number" : 110, "color" : "white" }
{ "_id" : { "$oid" : "54f92a357b2b4c977e827da5" }, "marina" : "Port Nicholson", "name" : "Pretty Lady", "number" : 515, "color" : "pink", "driven_by" : [ "sail" ] }
{ "_id" : { "$oid" : "54f92a3c7b2b4c977e827da6" }, "marina" : "Evans Bay", "name" : "Sally Ann", "number" : 313, "color" : "white", "driven_by" : [ "motor" ] }
{ "_id" : { "$oid" : "54f92a457b2b4c977e827da7" }, "marina" : "Evans Bay", "name" : "Charlie", "number" : 515, "color" : "blue", "driven_by" : [ "sail", "motor" ] }
{ "_id" : { "$oid" : "54fa7d642365c7e4bae69d0a" }, "marina" : "Upper Hutt", "name" : "Penquin", "number" : 131, "color" : "yellow", "driven_by" : [ "sail", "motor" ] }
收集4:
{ "_id" : { "$oid" : "54f92d567b2b4c977e827dae" }, "marina" : "Sea View", "reserves" : { "boat" : 313, "sailor" : 707, "date" : "2015-03-15" } }
{ "_id" : { "$oid" : "54f92d5c7b2b4c977e827daf" }, "marina" : "Sea View", "reserves" : { "boat" : 313, "sailor" : 111, "date" : "2015-03-16" } }
{ "_id" : { "$oid" : "54f92d627b2b4c977e827db0" }, "marina" : "Sea View", "reserves" : { "boat" : 111, "sailor" : 111, "date" : "2015-03-15" } }
{ "_id" : { "$oid" : "54f92d6c7b2b4c977e827db1" }, "marina" : "Sea View", "reserves" : { "boat" : 110, "sailor" : 818, "date" : "2015-03-15" } }
{ "_id" : { "$oid" : "54f92d707b2b4c977e827db2" }, "marina" : "Port Nicholson", "reserves" : { "boat" : 919, "sailor" : 818, "date" : "2015-03-16" } }
{ "_id" : { "$oid" : "54f92d757b2b4c977e827db3" }, "marina" : "Sea View", "reserves" : { "boat" : 515, "sailor" : 919, "date" : "2015-03-15" } }
{ "_id" : { "$oid" : "54f92d7a7b2b4c977e827db4" }, "marina" : "Port Nicholson", "reserves" : { "boat" : 919, "sailor" : 707, "date" : "2015-03-17" } }
{ "_id" : { "$oid" : "54f92d7e7b2b4c977e827db5" }, "marina" : "Port Nicholson", "reserves" : { "boat" : 616, "sailor" : 707, "date" : "2015-03-21" } }
{ "_id" : { "$oid" : "54f92d837b2b4c977e827db6" }, "marina" : "Port Nicholson", "reserves" : { "boat" : 515, "sailor" : 111, "date" : "2015-03-17" } }
{ "_id" : { "$oid" : "54f92d927b2b4c977e827db7" }, "marina" : "Evans Bay", "reserves" : { "boat" : 313, "sailor" : 818, "date" : "2015-03-19" } }
{ "_id" : { "$oid" : "54f92d977b2b4c977e827db8" }, "marina" : "Evans Bay", "reserves" : { "boat" : 515, "sailor" : 818, "date" : "2015-03-21" } }
{ "_id" : { "$oid" : "54f92d9e7b2b4c977e827db9" }, "marina" : "Port Nicholson", "reserves" : { "boat" : 818, "sailor" : 999, "date" : "2015-03-20" } }
{ "_id" : { "$oid" : "54f92da37b2b4c977e827dba" }, "marina" : "Port Nicholson", "reserves" : { "boat" : 818, "sailor" : 999, "date" : "2015-03-21" } }
{ "_id" : { "$oid" : "54f92daa7b2b4c977e827dbb" }, "marina" : "Port Nicholson", "reserves" : { "boat" : 717, "sailor" : 919, "date" : "2015-03-25" } }
提前致谢
答案 0 :(得分:0)
循环遍历collection4
并在循环内创建一个新对象,其中包含从其他集合上的findOne()
文档返回的字段:
db.collection4.find({"reserves.date": "2015-03-16"}).forEach(function(obj){
sailor = db.collection2.findOne({"sailorId" : obj.reserves.sailor});
boat = db.collection3.findOne({"number" : obj.reserves.boat});
doc = {
"Marina": obj.marina,
"Sailor": sailor,
"Boat": boat
};
db.new_collection.insert(doc);
});
使用新系列,您可以db.new_collection.find()
:
/* 0 */
{
"_id" : ObjectId("5523b4b59c752469483ce9bf"),
"Marina" : "Sea View",
"Sailor" : {
"_id" : ObjectId("54f92b2c7b2b4c977e827da9"),
"name" : "Peter",
"sailorId" : 111,
"skills" : [
"row",
"sail",
"motor"
],
"address" : "Upper Hutt"
},
"Boat" : {
"_id" : ObjectId("54f92a3c7b2b4c977e827da6"),
"marina" : "Evans Bay",
"name" : "Sally Ann",
"number" : 313,
"color" : "white",
"driven_by" : [
"motor"
]
}
}
/* 1 */
{
"_id" : ObjectId("5523b4b59c752469483ce9c0"),
"Marina" : "Port Nicholson",
"Sailor" : {
"_id" : ObjectId("54f92bdd7b2b4c977e827daa"),
"name" : "Milan",
"sailorId" : 818,
"skills" : [
"row",
"sail",
"motor",
"first aid"
],
"address" : "Wellington"
},
"Boat" : {
"_id" : ObjectId("54f92a2a7b2b4c977e827da3"),
"marina" : "Port Nicholson",
"name" : "Mermaid",
"number" : 919,
"color" : "white",
"driven_by" : [
"sail",
"motor"
]
}
}