Install the basic tutorials that use mongoose with node.js operation MongoDB _node.js

Source: Internet
Author: User
Tags findone mongodb mongodb driver mongo shell

Install Mongoose

Use Express to prepare a TESTMONGODB project with the following command sequence:

Express Testmongodb
CD testmongodb
npm Install

After you finish executing the above command, install mongoose using the following command:

NPM Install Mongoose--save

This command installs mongoose and relies on the project, and mongoose dependent MongoDB driver, regexp, and so on, are automatically installed.

Instance

With Mongoose, you can create a new database, create a new collection, and crud a document within a collection, and when you write code, you can verify that the results are as expected for the MONGO shell.

Under Testmongodb, create a new mongo.js file that reads as follows:

var mongoose = require (' Mongoose ');
Mongoose.connect (' mongodb://localhost/accounts ');
var db = mongoose.connection;
Db.on (' Error ', Console.error.bind (console, ' connection error: '));
Db.once (' Open ', function () {
 console.log (' Mongoose opened! ');
 var userschema = new Mongoose. Schema ({
   name:{type:string, unique:true}, 
   password:string
  }, 
  {collection: "Accounts"}
  );
 var User = Mongoose.model (' accounts ', Userschema);

 User.findone ({name: "Wanger"}, Function (Err, doc) {
  if (err) console.log (err);
  else Console.log (doc.name + ", password-" + Doc.password);
 });

 var lisi = new User ({name: "Lisi", Password: "123456"});
 Lisi.save (function (err, doc) {
  if (err) console.log (err);
  else Console.log (doc.name + ' saved ');
 }); 


The above file, you can view the effect directly by executing the node mongo.js command.

To use mongoose, first require, then connect to the database using the Connect method. Connect prototype:

Connect (URI, options, [callback])

The URI is in a similar format: "Mongodb://user:pass@localhost:port/database".

Mongoose's Connection object defines events such as connected, open, close, error, and so on, and we can listen to these events.

In our sample code, I listened to the Open event, defined the schema in the callback function, and called Mongoose.model to compile the schema to get the model object. It should be noted that the collection name specified when defining a schema is consistent with the first parameter of the Mongoose.model.

Get the Model object, you can perform additions and deletions to check and other operations. The model object has methods such as find (), FindOne (), update (), remove (), and so on, similar to our usage in the MONGO shell. These methods have an optional callback, and when you provide these callback, the results of the execution will be returned to you through this callback. If you do not, these methods return a query object, you can then assemble the new options via query, and then invoke query exec (callback) to submit the queries.

I used callback to look up Wanger's files in my code, and I didn't use query.

The model object has a model (DOC) method that is used to construct a document. This Document object's Save () method allows you to save the document to the database when you create the Lisi documents.
Basic operations through the Mongoose:
1, increase

var obj = new Movie ();
Obj.title = ' title one ';
obj.content = ' content ';
Obj.save (function (err) {
  
});

2. Delete

Movie.remove ({
  _id:id
},function (err) {
  
})


3, change

Movie.update ({
  _id:id
},json,{},function (err) {
  
})


4, check

Movie.findone ({
  _id:id
}, function (err, obj) {
 
});
Movie.find ({}). Sort ({_id:-1}). Limit (3). EXEC (function (err, obj) {
 
})

Related Article

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.