mongodb没有加入时如何找到我想要的结果?
Posted
技术标签:
【中文标题】mongodb没有加入时如何找到我想要的结果?【英文标题】:How to find the result I want when mongodb has not join? 【发布时间】:2015-06-11 18:40:47 【问题描述】:如果我的收藏为: 我的数据库是 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"
提前致谢
【问题讨论】:
您似乎只是在这里重新发布了an assignment question,而不是试图解决它。到目前为止,您尝试过哪些代码,您在哪里需要帮助? 【参考方案1】:循环通过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"
]
【讨论】:
以上是关于mongodb没有加入时如何找到我想要的结果?的主要内容,如果未能解决你的问题,请参考以下文章