Mongoose Study Notes 2--additions and deletions 1

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.