books表
db.books.insert( {_id:123, book_name:"Head First Java", author:"Kathy Sierra", publisher:"O'Reilly"} );
publishers表
db.publishers.insert( {_id:123, publisherName:"O'Reilly", location:"San Francisco"} );
关联查询:
db.books.aggregate([ { $lookup: { from: "publishers", ##从哪个表查询 localField: "publisher", ## foreignField:"publisherName", as:"publisher_data" } } ]);
mongos> db.books.aggregate([ ... { ... $lookup: ... { ... from: "publishers", ... localField: "publisher", ... foreignField:"publisherName", ... as:"publisher_data" ... } ... } ... ]); { "_id" : 123, "book_name" : "Head First Java", "author" : "Kathy Sierra", "publisher" : "O'Reilly", "publisher_data" : [ { "_id" : 123, "publisherName" : "O'Reilly", "location" : "San Francisco" } ] }
使用说明:
{ $lookup: { from: <collection to join>, localField: <field from the input documents>, foreignField: <field from the documents of the "from" collection>, as: <output array field> } }
标签:publisher,publisherName,...,mongodb,关联,books,Reilly,id From: https://www.cnblogs.com/hxlasky/p/17834410.html