Restrictions on drop collections on mongodb - mongodb

Is there a way to add a restriction when deleting collections that are referenced by id in other collections using mongodb or mongoose?
I need something like "on delete restrict" from sql to use on mongodb.

References from one collection to another are not explicit. So Mongo has no way to know that an ObjectId property in a document refers to the id of another document, nor in which collection. So I believe it's not possible to have such restrictions.

MongoDB doesn't support this feature, you can implement in your own application.
Keep a refcount field in your document to track referenced count.
db.coll.remove({refcount: 0})

Related

Default options for dynamic MongoDB collections

I am aware that MongoDB will dynamically create a collection when you attempt to insert a document into a non-existent collection. My question is:
How do I set up default options so that the collection has the options I want when it is dynamically created?
I don't think you can do that. Your options are:
Pre-create the collections if the pattern is dynamic yet predictable.
List the existing collections before inserting and create the new collection yourself with the right options if it's not already present.
Do not use dynamic collections but rather put the dynamic part as a property to individual documents in a shared collection.
I chose to write a wrapper around mongoc_database_get_collection(). There is a little bit of additional overhead for an extra round trip request to the mongo server to see if the collection already exists. If not, the wrapper creates it with the options I want and returns the new collection. If the collection already exists, the wrapper returns the existing collection.

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

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.

Is it okay to use MongoDB _id outside of MongoDB (Mongoose) context?

I am using MongoDB with Mongoose.
I was wondering, if it is bad practice to use MongoDB IDs outside of MongoDB context. Since a lot of my objects need an ID to be identified, I was wondering if I just could use the IDs MongoDB gives them anyway or is that bad practice?
Best regards
What I understand from your question is that if have a document from Mongodb that becomes an object in your application. To identify this object across the application, you want to use this _id so that changes to this object can be tracked easily. If this is the case, you should be using it happily. Because the ObjectId's of Mongodb are unique.
Infact, I do use this _id in my android application. example code here
studentUniqueId.setText(dataModelItem.get_Id());
where studentUniqueId is a hidden field in my android application.

Restrict document fields addition in MongoDB?

Does MongoDB provide any mechanism to restrict addition of a new field to a document in a collection?
I would think that the best way to do that would be using some kind of "add new field" privilege, but it seems that MongoDB has got just two roles - readonly and normal. Is there any other way to "lock" a collection?

Why does MongoDB have collections

MongoDB being document-oriented, the structure of collections seems to be a special case of documents. By that I mean one can define a document to contain other documents. So a collection is just a document containing other documents.
So why do we need collections after all?
Logically yes, you could design a database system like that, but practically speaking no.
A collection has indexes on the documents in it.
A collection requires the documents in it to have unique ids.
A document is limited in size.
Object ids (_id top-level document attribute) must be unique within a collection. Multiple collections may have the same _id, just like in RDBMs where the key constraint is per-table, yet multiple tables may contain the same value for a key.
collections is a container for documents. so when you say a document that contain other documents that s kinda wrong because, already, a document can have inner documents.
Collection is the unit where you put together the documents. Be aware that due to schema free design, you can put anything in a collection but it s not a good design. so collection is kinda logical container for documents. same as tables in relational world.