The previous blog wrote a collection of management, the collection is stored in the document, so smart you should be able to think of this is learning document management. To say the title should be document management, but the management of the document is to get the collection object first, the method management document is called on the collection object, so the title is also the management of the collection.
In collection management, to manage a collection first to obtain this collection, the same document management is to obtain the collection object first, and then manage the document through the collection object.
I. Adding a document to the collection
Why first say add, because do not add can not say the following search. To add a document to a collection, you first need to get the collection object and invoke the Insert (document) or save (document) method from several objects. The Document object is a correctly formatted JS object that is converted to Bson and stored in the collection. The following figure uses insert, save two ways to add a document.
Second, find the document in the collection
Find English words have search, find and so on (learning English, after all, I am not a point four, you may ask besides these two there are other? After all, use is waiting, in fact I want to tell you my English Dictionary of these two, the other I really do not know there is no). Okay, here's the end of the chatted. Looking in the collection didn't think it was with find, it's not over my knowledge. You can see that you use the Find method to find a document. query specifies that the containing field and the document worth querying match the documents in the collection.
Iii. Deleting a document from the collection
Delete is removed by calling remove ([query]) from the collection object. However, if you use Remove () to delete without query, you will get an error, for example, if you really want to delete all you can use remove ({}) to pass an empty JS object.
Two documents were added to find the added document in the Learning collection. A document is added here to make it easier to remove the viewing effect.
As can be seen in the above figure, the direct use of remove () is an error. Use Remove ({}) to delete all documents.
Iv. updating documents in the collection
In fact, the management of the collection in the first blog http://www.cnblogs.com/5ishare/p/5628126.html also has a simple mention, this time or simple to say, more complex later will listen to tell.
You should first get the collection to update the document in the collection. Then use the Save (object) method to save changes to an object. You can also use the update (query,update,options) method to query the documents in the collection, and then update them when they are found. The query parameter specifies a search document that matches the field and value to the document that the collection is in. The update parameter is an object that specifies the update operator that is used when an update is made. Increments the value of the field, such as $inc. $ sets the value of the field, $push pushes an entry to the array, and so on. The option parameter is an object that has two Boolean properties, multi and Upsert. Upsert is true when a new document is created if it is not found. If multi is true, all documents that match are updated, otherwise only the first document is updated.
In the first 3 documents are created, and then update age=25, but Multi=false, so only the first document is updated, there is a age=25.
It's also interesting to create a document, before it was created as a single one, just tried to create it in bulk, and the following figure also saw the way to add it as an array.
MongoDB Collection Management II