MongoDB's Nodejs driver base

Source: Internet
Author: User
Tags mongoclient

Learning to write MongoDB, using the Nodejs. Here I put the hole first record, later can slowly find. This article is also slowly, for the sake of brevity, all omit the handling of errors.

Basic operations:

1. Increase

1 varMONGO = require (' MongoDB '). Mongoclient2 varName = {3City: ' Wuhan ',4Country: ' China '5 }6Mongo.connect (' Mongodb://localhost:27017/test ',function(err, db) {7     varCollection = Db.collection (' Docs ')8Collection.insert (Name,function(err, data) {9 Console.log (json.stringify (name))Ten db.close () One     })     A})

Insert is simple, inset can be. But notice the second parameter of the callback function: data, which is not what we insert, but is wrapped. In this example, I print the property name and value of data, which is the object:

{    result: {        1,        1    },    ops: [{        ' Wuhan ',        ' China ',         c3f78ea3104d15f8125564    }]    ,1,    insertedids: [58 c3f78ea3104d15f8125564]}

You can see that all the information that has been inserted is returned, the inserted document is wrapped by OPS and is an array of numbers. If a single insert is the first element in the array.

2. By deleting

Deleting is also easier, but adding a query to the process. On the code:

 1  var  MONGO = require (' MongoDB '  2  mongo.connect (' Mongodb://localhost:27017/test ', function   (err, db) { 3  var  collection = Db.collection (' Doc ')    Span style= "COLOR: #000000" >)  4   Collection.remove ({ 5  City: ' Wuhan ' 6
    }, function   (err, data) { 7   Db.close ()  8  
    
     }) 
     9 }) 

With remove, the first parameter is the deletion condition, where we delete the entry with the city as Wuhan.

3. Check

With the experience of deletion, it is easy to check.

var mongo = require (' MongoDB '). Mongoclientmongo.connect (function(err, db) {    var collection = Db.collection (' Doc ')    collection.find ({        "age": {            "$GT": $        }    }, {        1  ,        1,        0    }). ToArray (function(err, doc) {        db.close ( )    })})

We use conditional lookups here, the first parameter is to find an entry with age greater than 30, and the second is to return only name and age, not return _id. The third is a callback function, and the second parameter passed in is the object to be found, arranged in an array. Returns an empty array if it is not found, and finds several entries to return a few.

4. Change

The update operation is slightly more difficult. More complex.

var mongo = require (' MongoDB '). Mongoclientmongo.connect (function(err, db) {    var collection = Db.collection (' Doc ')    collection.update ({        ' Wuhan '    }, {        $set: {            ' Hubei '        }    }, function (err, data) {        db.close ()})    })

This is the simplest operation, first find the city for Wuhan entries, and then add these entries province as Hubei key-value pairs.

5. Array manipulation

For the time being only use the $push method, will certainly use more and more, similar to the Update method, but not $set.

var mongo = require (' MongoDB '). Mongoclientmongo.connect (function(err, db) {        var collection = Db.collection (' Question ')        collection.update ({            ' Hubei '        }, {            $push: {                ' Huangshi '             }         function (err, data) {            db.close ()})    })

This paragraph can be used as the original

1 {2     Province: ' Hubei ',3     cities: [' Wuhan ']4 }

Adds an attribute to the

1 {2     Province: ' Hubei ',3     cities: [' Wuhan ', ' Huangshi ']  4 }

This is the push operation.

MongoDB's Nodejs driver base

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.