[SERVER-11865] A $findOne operator for $let,$map in $project phase Created: 26/Nov/13  Updated: 03/Feb/16  Resolved: 04/Nov/15

Status: Closed
Project: Core Server
Component/s: Aggregation Framework
Affects Version/s: None
Fix Version/s: None

Type: New Feature Priority: Major - P3
Reporter: Zev Teh Assignee: Unassigned
Resolution: Duplicate Votes: 3
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates SERVER-19095 $lookup Closed
Participants:

 Description   

A $findOne operator should be useful when data must be compiled from multiple collections.
This is not like a "Collection JOIN Collection".
It is more towards "From this document LOOKUP that document".

	$findOne: {Options, Criteria, Projection}

Options is

{Collection:"name", Database:"db"}

Criteria and Projection will be similar to db.collection.findOne() method.

Take following sales order document for example:

db.sales.findOne()

	{
		"_id" : 1,
		"refcd" : "S1",
		"date" : ISODate("2010-11-26T11:18:16.463Z"),
		"cust" : {
				"cid" : 10,
				"doc" : "customer"
		},
		"lineitem" : [
				{
						"rowid" : 1,
						"prod" : {
								"pid" : 20,
								"unitprice" : 41.37,
								"doc" : "product"
						},
						"qty" : 26,
						"linetotal" : 1075.62
				}
		],
		"amount" : 1075.62
	}

To get the details from customer & product details for this document:

	db.sales.aggregate([
	{ $project: 
			{
				"_id":1,
				"refcd":1,
				"date":1,
				"cust.cid":1,
				"cust.doc":1,
				"cust.ref":{$let: {vars:{}, in:{$findOne:{
														{Collection:"$cust.doc"},
														{"_id":"$cust.cid"},
														{"_id":0}
														}
												}
								} 
						},
				"lineitem":1,
				"lineitem_ref": {$map: { input: "$lineitem",
										as: "li",
										in: {$findOne:{
														{Collection:"$$li.prod.doc"},
														{"_id":"$$li.prod.pid"},
														{"_id":0}
														}
											}
									}
								},
				"amount":1
			}
	}
	])



 Comments   
Comment by Charlie Swanson [ 04/Nov/15 ]

I'm closing this as a duplicate of the new $lookup aggregation stage (see documentation), as it appears to satisfy the request.

If this does not meet your use case, feel free to re-open, or to file a new ticket. Thanks!

Generated at Thu Feb 08 03:26:57 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.