Inquire
Before our collection has been created successfully, let's start with the first step--query.
There are many types of queries, such as conditional query, filter query, and so on, today we only learn the most basic find query.
Example :
1.find query: Obj.find (Query conditions, callback);
Model.find ({},function (Error,docs) {//If no parameters are passed to find, the default is to show all documents}); Model.find ({"Age":-}, Function (Error, Docs) {if (error) {Console.log ("Error:" + error);} Else{console.log (docs);//docs:age is 28 of all documents});
Model Save method
The model provides a create method to save the data. Let's look at an example:
1. Model.create (document data, callback))
Model.create ({name: "Model_create", age:26}, function (Error,doc) {if (error) {Console.log (error);} else {Console.log ( (doc);}});
Entity Save method
Just learned the model of the Create method, then begin to learn the method based on entity preservation. The following example:
1. Entity.save (document data, callback))
var Entity = new Model ({name: "Entity_save", age:27}); Entity.save (function (Error,doc) {if (error) {Console.log (error);} else {console.log (doc);}});
Data Update
Learn the preservation of data, and then we begin to learn to update the data it!
1. Example: obj.update (query condition, update object, callback);
var conditions = {name: ' Test_update '}; var update = {$set: {age:16}}; Testmodel.update (conditions, UPDATE, function (Error) {if (error) {Console.log (error);} else {console.log (' Update Success! ');});
Delete data
With the data to save, update, the difference is deleted, below we come to learn it!
1. Example: Obj.remove (query condition, callback);
var conditions = {name: ' Tom '}; Testmodel.remove (conditions, function (error) {if (error) {Console.log (error);} else {console.log (' Delete success! ');}});
Course Summary
In this chapter, we describe several methods of operation for the database, by invoking the relevant methods to change the data, whether it is new, delete, modify or check query, you can do.
Brief review:
1. Query: The Find query returns one, multiple, or empty array document results that match the criteria.
2. Save: Model calls the Create method, and the entity invokes the Save method.
3. Update: obj.update (query condition, update object, callback), update the relevant data according to the condition.
4. Delete: Obj.remove (query condition, callback), delete the relevant data according to the conditions.
Mongoose Study Notes 2--additions and deletions 1