MonGoDb simple additions and deletions to search

Source: Internet
Author: User
Tags mongoclient mongodb

One: Start the database service
1:cmd Enter the bin directory of the database installation directory
2:MONGPD--dbpath Database data storage directory
Example: Mongod--dbpath E:\NODE\MonGoDB
database Data storage directory for any existing directory

Two: Connect a database service that has started
1:cmd into the bin directory of the database installation directory (the window opened in the first step cannot be closed and stopped)
2:mongo Database access address: Port
Example: MONGO 127.0.0.1:27017
The local access address is 127.0.0.1 or localhost
The default port is 27017

Three: Common commands
1:show DBS, see all databases that have been created
2:use Database name--switch or create a database
3:db.dropdatabase (); Delete current database
4:db.getname (); Get the name of the current database
5:db.getmongo (); Get the connection address of the current database
7:show cellections, getting all collections of the current database (table)
8:db. Collection name. Save (JSON), add data to the collection (table), the nonexistent collection is automatically created
9:db. Collection name. Update (Json1, json2), modifying data
Json1 for query criteria such as {"id": +}
Json2 to modify as {"name": "Test"}
modifies the name field of all data with ID 25 to "test"
10:db. Collection name. Remove (JSON), delete data
JSON is similar to the json1 of 9 for conditions that need to be removed
11:db. Query data with name. FIND (JSON)
JSON is similar to 9 json1, for conditions that require querying

Four: code example
Let mongoclient = require ("MonGoDB"). Mongoclient;let URL= "Mongodb://localhost:27017/admin"; mongoclient.connect (URL,function(err, db) {if(Err) {Console.log ("Database connection Failed!"); } Else{insertone (db); }});functionInsertone (db) {db.collection ("User"). Insertone ({"Name": "Test",        "Sex": 1    }, function(err, result) {Console.log (Result.result);    Console.log (Result.ops) db.close (); });}
Let mongoclient = require ("MonGoDB"). Mongoclient;let ObjectId= Require ("MonGoDB"). Objectid;let URL= "Mongodb://localhost:27017/admin"; Mongoclient.connect (URL,function(err, db) {if(Err) {Console.log ("Database connection Failed!"); } Else{deleteData (db); }});functionDeleteData (db) {db.collection ("User"). Deleteone ({_id:NewObjectId ("59ba30b2d5f3741b9831bea8")    }, function(err, result) {Console.log (Result.result);    Db.close (); });}
Let mongoclient = require ("MonGoDB"). Mongoclient;let URL= "Mongodb://localhost:27017/admin"; mongoclient.connect (URL,function(err, db) {if(Err) {Console.log ("Database connection Failed!"); } Else{uodatedata (db); }});functionUodatedata (db) {varcursor = db.collection ("User"). Updatemany ({"Name": "Zhang San"    }, {        "$set": {            "Name": "Chen Hui",            "Sex": 1        }    }, function(err, result) {Console.log (Result.result); });}
Let mongoclient = require ("MonGoDB"). Mongoclient;let ObjectId= Require ("MonGoDB"). Objectid;let URL= "Mongodb://localhost:27017/admin"; mongoclient.connect (URL,function(err, db) {if(Err) {Console.log ("Database connection Failed!"); } Else{selectall (db); }});functionSelectAll (db) {//Exact Search    //var cursor = db.collection ("User"). Find ({_id:new ObjectId ("59ba30970692cd172083a836")});    //Search All    varcursor = db.collection ("User"). find (); varresult = []; Cursor.each (function(err, doc) {if(doc = =NULL) {Console.log (result);        Db.close (); } Else{Result.push (doc); }    })}

MonGoDb simple additions and deletions to search

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.