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) {
})