Reputation: 3965
I have two different collections with a common field say, a UserId. There are also other attributes that qualify the UserIds.
For Example:
Collection 1: {UserId, SellsToUserId}
Collection 2: {UserId, BuysFromUserId}
I want to run an operation that gives me the difference between two sets.
A sample query would be: Get all UserIDs that a given UserId sells to, but does NOT Buy From.
Solution in pseudocode
var sellToCursor = collection1.Find(Query.EQ("UserId", Me)).SetFields({SellsToUserId});
var buyFromCursor = collection2.Find(Query.EQ("UserId", Me)).SetFields({BuysFromUserId});
SellToButDontBuyFrom[] = sellTo - buyFrom; //definitely pseudocode here.
I want to do this on the MongoDB server, because I have large sets of data.
Any suggestions to do this in an efficient way?
Upvotes: 4
Views: 575
Reputation: 3717
You can perform the same logic using javascript and executing it on the server, but it will not be faster. If your C# client has a fast bandwidth to the server, it will be much better choice. To optimize it, you can sort both of the queries by SellsToUserId and BuysFromUserId respectively and iterator through two cursors similar to merge sort algorithm where you can stop when sellToCursor reaches the end.
Upvotes: 3