Making a given difference in MongoDB

I have two different collections with a common field, like UserId. There are other attributes that qualify UserIds.

Example:

Collection 1: {UserId, SellsToUserId}

Collection 2: {UserId, BuysFromUserId}

I want to start an operation that gives me the difference between two sets.

Sample request: Get all user IDs that this UserId sells but does not buy.

Pseudo code solution

 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 a MongoDB server because I have large data arrays.

Any suggestions for effective use?

+4
source share
1 answer

You can execute the same logic with javascript and execute it on the server, but it will not be faster. If your C # client has fast server bandwidth, this would be a much better choice. To optimize it, you can sort both queries with SellsToUserId and BuysFromUserId respectively, and the iterator with two cursors, similar to a merge sort algorithm, where you can stop when sellToCursor reaches the end.

+3
source

Source: https://habr.com/ru/post/1385789/


All Articles