Find oldest for every joined document - mongodb

I have two collections, A and B. Each document in collection A corresponds to many documents in collection B. Documents in collection B have two important properties, "AID", the ID of the corresponding collection A document, and "date", something I want to sort by.
I now have a find query on collection A, which returns many documents in that collection. For each of the returned documents, I now want to find the oldest corresponding document in collection B.
So far, I used a for-each on my find query in collection A, and then used a find({"AID": doc._id}).sort({"date": 1}).limit(1) on collection B.
This is obviously super inefficient, since I have to go through my enormous collection B every single time for each document in collection A. Can I somehow simplify this into two aggregated queries only, perhaps using some kind of aggregate pipeline? How can I only traverse collection B once?
Thanks!

Related

query in mongodb atlas to verify the existence of multiple specific documents in a collection

I have a mongodb collection called employeeInformation, in which I have two documents:
{"name1":"tutorial1"}, {"name2":"tutorial2"}
When I do db.employeeInformation.find(), I get both these documents displayed. My question is - is there a query that I can run to confirm that the collection contains only those two specified documents? I tried db.employeeInformation.find({"name1":"tutorial1"}, {"name2":"tutorial2"}) but I only got the id corresponding to the first object with key "name1". I know it's easy to do here with 2 documents just by seeing the results of .find(), but I want to ensure that in a situation where I insert multiple (100's) of documents into the collection, I have a way of verifying that the collection contains all and only those 100 documents (note I will always have the objects themselves as text). Ideally this query should work in mongoatlas console/interface as well.
db.collection.count()
will give you number of inserts once you have inserted the document.
Thanks,
Neha

How can I filter different key words from one collection into another one?and put them together.

I used the aggregate, match and out query, however, if I want to filter C and also put into B collection, there will be a problem that A will disapper, only left C.
So do you have any query that I can filter from "location" collection and put my keywords.A,c and others all into B collection?
It sees $out only can have one keyword

mongo: finding missing documents by propery value

I have two collections with documents of identical schema. Let's call them collections A (with documents {a1, a2,...,am}) and B (with documents {b1, b2,...,bn}). Is there a way to construct a mongo query such that it returns a set of documents {a1, a2,.., ak} from collection A where ai<m[p] ∉ UNIQUE(bj<n[p])?
In reality collection B has a few dozen million records and collection A has several hundred million records. I can't figure out how to do this efficiently.

MongoDB $in not only one result in case of repeated elements

I need to get the users whose ids are contained in an array. For this i'm using the $in operator, however being this inside an aggregate operation, i'd like to get back a specific user all the time it's id is present in the array, not just one. For example:
The ids array is A=[a,b,c,b] and U(x) is user with id x
with users.find({_id:{$in:A}}) i get these users as result: U(a),U(b),U(c)
instead i'd like to get back the result: U(a),U(b),U(c),U(b)
so get the user back every time it's id appears.
I understand that $in is working as expected but does anyone have an idea on how can i achieve this?
Thanks
This isn't possible using a MongoDB query.
MongoDB's query engine iterates over the documents in a collection (or over an index if there's a useful one) and returns to you any documents that match your query, in the order it finds them. Whether b appears once, twice, or a hundred times in your query makes no difference: the document with _id of b matches the query and is returned once, when MongoDB finds it.
You can do a post-processing step in your programming language to repeat documents as many times as you want.

Mongo: Fetching documents that aren't referenced in another collection

Lets assume that I have two collections in my Mongo database: A & B. Each A document may have reference to B, but B documents don't have references back to A.
How can I efficiently find all documents in B that are not referenced by a document in A?
Is there a more effective approach than retrieving all documents in B and manually comparing against A documents? Can this be done with map reduce?
Should I consider adding references from B to A to support the query? Since Mongo doesn't support transactions, I had avoided any two way references to avoid any chance of an inconsistent state in the event of a failure.
Also, I need to be able to effectively page through these results, if that impacts the solution.
In pseudo-code:
// Get the set of B document ids that are referenced by A documents.
var bref_ids = db.A.distinct('b_id');
// Get the set of all other B documents.
var unreferenced_b_docs = db.B.find({_id: {$nin: bref_ids}});