This article focuses on installing the basic tutorial using Mongoose to let node. js operate MongoDB, front-end js+ back-end Node+js Operation MongoDB is the so-called most popular kind of JavaScript full stack development program, need friends can refer to the next
Installing Mongoose
To prepare a TESTMONGODB project using Express, the command sequence is as follows:
express TestMongoDB
cd TestMongoDB
npm install
After executing the above command, use the following command to install mongoose:
npm install mongoose --save
This command will install mongoose and use it as a project dependency, and the MongoDB driver and regexp modules that mongoose depends on will also be automatically installed.
Examples
Using mongoose, you can create new databases, create new collections, and perform CRUD operations on the documents in the collections. When writing code, you can verify whether the results meet the expectations against the mongo shell.
Create a new mongo.js file under TestMongoDB with the following content:
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, directly execute the "node mongo.js" command to view the effect.
To use mongoose, first require, and then use the connect method to connect to the database. Connect prototype:
connect (uri, options, [callback])
The format of uri is similar: "mongodb: // user: [email protected]: port / database".
The connection object of mongoose defines some events, such as connected, open, close, error, etc., 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 name of the collection specified when defining the Schema must be consistent with the first parameter of mongoose.model.
Once you get the Model object, you can perform operations such as adding, deleting, modifying, and checking. Model objects have find (), findOne (), update (), remove () and other methods, similar to our usage in the mongo shell. These methods have an optional callback, when you provide these callbacks, the results of the execution will be returned to you through this callback. If you do not provide them, these methods will return a Query object, you can then assemble new options through Query, and then call Query exec (callback) to submit the query.
When I searched WangEr's file in the code, I used callback instead of Query.
The Model object has a Model (doc) method, used to construct a document (Document). When creating a Lisi document, it is this save () method of the Document object that can save the document to the database.
Basic operations through 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) {
})
Install and use Mongoose with Node.js to operate the basic tutorial of MongoDB [Reprinted]