I am trying to fetch the most recent document in MongoDB using mongoose.
Every method I have tried so far gives me the oldest document on record.
I've tried sorting by date, and created_at
Tried both ascending and descending, both return the oldest document.
https://paste.heckyou.ml/odofomasel.js
The value returned should be the most recently added document.
This will give you one last document for a collection
db.collectionName.findOne({}, {sort:{$natural:-1}})
$natural:-1 means order opposite of the one that records are inserted in.
mongo CLI syntax is: db.collectionName.find({}).sort({$natural:-1}).limit(1)
Related
I actually want To display Full documents Without duplication
When I use distinct i can only display one particular field
How to display all fields without selecting that document which is repeated
Each document in MongoDB contains an _id field which must be unique in the collection. Hence, it is not possible to have two identical documents in a collection. Ergo, iterating a collection with no conditions will return all unique documents (which are all documents in the collection).
I'm building a mongoDB database that holds sales data from multiple different systems. Each system is integrated via an node/mongoose/Express API that I'm creating for the database. Typically, you'd check the id to determine if a record already exists, and insert it if it doesn't. But since the ID from these different sources could technically overlap, I need a system to make sure that a source can only update records that originally came from that source. So I've added a column called "external_ID" where the record id from the source is saved, and another column called "integration ID", which will be unique to the specific system that sends data. But for that idea to work, I'd need to update only if those two columns matches, and otherwise insert a new record. Is that possible with MongoDB, or am I approaching this wrong?
Thank you so much.
Use upsert on update(). It will creates a new document when no document matches the query criteria.
db.collection.update(<query>, <update>, { upsert: true })
You can find more detail at Upsert Behavior documentation
I have application which has solr search facility. When i query for search result comes only fields which has the values. How to get the result with empty field values in solr? How to get all the fields in result even though the value of a field is empty?
Only the fields present is stored with each document, so if you want to keep empty fields actually available, you'll have to explicitly index empty content into the field. How you do that depends on how you're indexing documents to Solr today.
You can work around this by fetching the schema for the core / collection you're querying first, retrieve the field names and adding empty fields for each field present in the schema, if necessary.
It'll probably be easier to assume that missing fields are empty, and wrap your code in a check to see if the field is present, and if not, return an empty value instead.
The reason for this is that while Solr uses a schema, the underlying Lucene library does not - any document can contain a set of field names unrelated to the other documents present in the index. Since the schema can change independent of the content of the index as well (a schema change will not update existing documents), returning non-existing fields isn't straight forward - and for most cases the document returned should be as close to possible to what was actually indexed.
I have a collection in mongodb that has more than 100000 documents. Each time I apply Model.find(), it returns all documents in an array.
But for some reasons like unnecessary data or performance, speed of the query, I just want to find document that is:
Starting with first document
The second document will be the 300th document
The third document will be the 600th document
...
The final document will be the 100000th document
Is there any mongoose query for that?
I have a collection in CosmosDB that has a large number of JSON files. I have a python program that continuously writes and uploads data to that collection. My format of the data just changed, so I am now writing files with a new structure. I have to delete all the files in my collection with the old structure.
Question 1: Do the documents have a date of creation tag? If so, I would like to delete all the files that have a date of creation earlier than a specific date. How can I do that?
Question 2: If the answer to the previous question is no, there is a way I can query parts inside all the old files I want to delete. I cannot query the files entirely, but I can query what's inside of them. So is there a way to delete the entire documents based on the query of what's inside of them? Maybe if there is a way to retrieve all the document IDs that are used to respond to my query, then it would be possible.
All documents have a property called _ts which is the unix timestamp of when the document was created and is auto populated by Cosmos. You should be able to query using this property to find all the documents created before a specific date.