Get latest two documents in Mongo aggregagte query - mongodb

I am using aggregate query to fetch multiple data from various Collections. Some of the collections have more than one records, I there a way where I can get latest(by latest i mean recently added) two records from particular collection.

Related

Search Document from multiple collection using Flutter

Can anyone tell me if it is possible to search multiple collections at once in Firebase?
What I need to do is search the data in both the 2009 and 2010 collections in bio_data (more collections will need to be added in the future) and extract the document from one of them.
Or it doesn't matter if there is a way to get a name list of the collections inside a document
Firestore does not have the concept of server-side joins or projections across collections. Each query or document read can only take data from a single collection, or from all collections that have the same name with collection group queries.
If you need to load data from two collections, you'll need at least two reads.
For a better understanding I highly recommend watching the Get to know Cloud Firestore playlist.
Michel's answer is correct.
In addition, I would suggest a change to your data model: Instead of having one sub-collection per year, you could have one unique sub-collection and add a year field to the docs in this unique collection.
This way it would be easy to query by year: For one year, for several years with the in operator (up to 10 equality (==) clauses) or for all the years.
And with a Collection Group query you could even query all the data for all the students.

How to use Atlas search on multiple collections

I have two collections, that are store on Atlas.
I want to be able to search for a text, using the regex operator of Mongo Atlas Search on these two collections, but with only one aggregation.
These two collections have nothing in common, so I can't do a $lookup on it.
I need to do this search in one aggregation because the result of this aggregation must be paginated with $limit and $skip in the aggregation.
How to perform that ?
You can use Atlas Data Lake on top of these two collections to federate queries across both. To do this you would create a Data Lake, create a virtual collection in your Data Lake, and then have both of these collections listed as "datasources" for that virtual collection.
The Data Lake infrastructure will push down queries to the underlying cluster and union the results before returning them to the client application.
In this example there are multiple types of data sources, but you can just list multiple cluster collections.
https://docs.mongodb.com/datalake/reference/format/data-lake-configuration/#example-configuration-for-running-federated-queries

How to query documents from an entire MongoDB database containing multiple collections at once (Pymongo)

I have a DB containing thousands of collections, each collection containing documents.
How would I query all these documents from all the collections at once?
I have considered using the $lookup aggregation method but from my knowledge, it only combines an additional collection at once, whereas I have thousands of collections that I want to combine and query altogether.
Is there a way to achieve this?
MongoDB operations work on a single collection at a time. As you point out, $lookup can perform a cross-collection lookup, but it won't help you with a large number collections.
Re-design your data model.

Duplicate efficiently documents in MongoDB

I would like to find-out the most efficient way to duplicate documents in MongoDB, given that I want to take a bunch of documents from an existing collection, update one of their field, unset _id to generate a new one, and push them back in the collection to create duplicates.
This is typically to create a "branching" feature in MongoDB, allowing users to modify data in two separate branches at the same time.
I've tried the following things:
In my server, get data chunks in multiple threads, modify data, and insert modified data with a new _id in the base
This basically works but performance is not super good (~ 20s for 1 million elements).
In the future MongoDB version (tested on version 4.1.10), use the new $out aggregation mechanism to insert in the same collection
This does not seem to work and raise an error message "errmsg" : "$out with mode insertDocuments is not supported when the output collection is the same as the aggregation collection"
Any ideas how to be faster than the first approach? Thanks!

How to apply sorting in multiple collections with same structure in mongo db?

In my mongodb database have no. of collections with same structure (Actually due to size ,split the original collection in to several collections based on date , every 2 days a new collection will generate ).
Currently I have to apply sorting and pagination in these collections.