Can an ordered array inside a document with MongoDB be guaranteed safe to maintain order in production [duplicate] - mongodb

Simple question, do arrays keep their order when stored in MongoDB?

yep MongoDB keeps the order of the array.. just like Javascript engines..

Yes, in fact from a quick google search on the subject, it seems that it's rather difficult to re-order them: http://groups.google.com/group/mongodb-user/browse_thread/thread/1df1654889e664c1

I realise this is an old question, but the Mongo docs do now specify that all document properties retain their order as they are inserted. This naturally extends to arrays, too.
Document Field Order
MongoDB preserves the order of the document fields following write operations except for the following cases:
The _id field is always the first field in the document.
Updates that include renaming of field names may result in the reordering of fields in the document.
Changed in version 2.6: Starting in version 2.6, MongoDB actively attempts to preserve the field order in a document. Before version 2.6, MongoDB did not actively preserve the order of the fields in a document.

Related

MONGODB - Add duplicate field with different value

Is there a way to write a script that updates a document by adding a duplicate field with a different value? I cannot use set as that replaces the existing value. I cannot use push as the field is in an object, not an array. I even tried creating the new field with a different name and renaming it which also replaces the existing field.
You cannot have duplicate fields in a Mongo record. A Mongo collection is a collection of documents, otherwise known as objects. You cannot have a duplicate field in an object and Mongo is no different.
MongoDB (and any other database that I have come across so far) is built around the idea that individual fields are identifiable so they can be filtered by, grouped by, sorted by, etc... That also explains why MongoDB does not provide support for the scenario you're facing. That being said, MongoDB can be used as a dumb datastore for arbitrary JSON data. And the JSON specification does not say anything about duplicate field names which is probably why you can actually store such a document in MongoDB in the first place.
Anyway, there is no way to achieve what you want without loading the entire document, changing it (by adding the duplicate field(s)) and then replacing the whole document. That, however, will work.
I personally cannot think of a reasonable scenario where this sort of document could make sense, though. So I would strongly suggest you revisit your document structure.

What's the difference between Drop and Remove in Mongodb? [duplicate]

This question already has answers here:
MongoDB: Trade-offs of dropping a collection vs. removing all of its documents
(4 answers)
Closed 5 years ago.
I see there's two ways to remove collections in Mongodb. I just want to delete it essentially.
Should I use:
db.collection.drop()
Or
db.collection.remove()
Once we have documents stored in our collection , we can remove all of the documents from it in two ways. Now choosing one over another is totally depends on your requirement.
1. Using drop():
By invoking drop() on a collection , it will remove all the documents from it ,it will delete all the indexes on it and at the end it will delete the collection itself.
2.Using remove():
remove has two overloaded versions ,one in which we will pass the criteria to remove all the documents that are matching our passed criteria and 2nd one is default where we won’t pass any criteria (prior to 2.6) or pass an empty document (version 2.6 or more) and it will remove all the documents from the collection. Here, we are more interested in 2nd version when our intention is to clear all the documents from a collection.
Remark: To remove all documents from a collection, it may be more efficient to use the drop() method to drop the entire collection, including the indexes, and then recreate the collection and rebuild the indexes.
Based on the documentation, remove()
Removes documents from a collection.
but doesn't get rid of the collection (or associated indexes). remove() can take parameters to specify deletion criteria as well.
drop() gets rid of the collection and associated indexes:
Removes a collection or view from the database. The method also removes any indexes associated with the dropped collection. The method provides a wrapper around the drop command.
https://docs.mongodb.com/manual/reference/method/db.collection.remove/
https://docs.mongodb.com/manual/reference/method/db.collection.drop/
It's a little bit like the difference between DELETE (or maybe TRUNCATE) and DROP TABLE/VIEW in relational SQL.

MongoDB: How to remove all documents matching a query and return their ids

With MongoDB, is it possible to specify a query and removes all matching documents while returning the documents' ids (alternatively the whole document)?
I found "How to get removed document in MongoDB?" that explains how remove a single document and return it using findAndModify. However, I need to remove a bunch of documents at once.
I'm ok with a solution involving the aggregation pipeline as long as it fulfils the requirements.
I'm using the offical C# driver, but solutions in JS are ok.

Are arrays in MongoDB documents always kept in order?

I can find ample evidence that MongoDBs are always kept in the order given when a document is inserted or updated, but I just can't find it explicitly spelled out in the documentation for MongoDB.
So I'll go ahead and ask the [probably stupid] question: are arrays in MongoDB documents kept in the order specified when they were inserted/updated?
And sharding/replication/etc. do not effect this ordering, correct?
If you are speaking about the order of an array value of a document field, then yes the order is always kept. See this question.
If you speak about the order of documents in a collection, to my knowledge you shouldn't count on any order. If you need an order, you should implement your own created_at or sequence_index field.

MongoDB: getting ObjectId of last inserted document with multiple, concurrent writers?

Consider the following scenario with MongoDB:
Three writers (A,B,C) insert a document into the same collection.
A inserts first, followed by B, followed by C.
How can we guarantee A retrieves the ObjectId of the document he inserted and not B's document or C's document? Do we need to serialize the writes (i.e., only permit B to write after A inserts and retrieves the ObjectId), or does MongoDB offer some native functionality for this scenario?
Thanks!
We're on Rails.
the normal pattern here is for the driver to allocate the ObjectId and then you know what it is for the insert even before the server gets it.
You can generate the _id value in your client applications (writers) before inserting the document. This way you don't need to rely on the server generating the ObjectId an retrieving the correct value. Most MongoDB language drivers will do this for you automatically if you leave the _id blank.