MongoDB installation start adding additions and deletions to the document

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

Add the bin path to the environment variable after the MongoDB decompression

Create A.bat and B.bat files:

A.bat content:

Mongod--dbpath F:\MongoData

B.bat content:

MONGO 127.0.0.1:27017/admin

A.bat is the boot MongoDB server,--dbpath is used to specify the storage path of the data

B.bat is the start of the MONGO shell (ie: JS engine), theadmin is used to specify which database

Start A.bat and see

2014-10-14t22:35:48.734+0800 [Initandlisten] waiting for connections on port 270
17

The instructions are OK.

Don't shut the window, that's MONGO's server.

Re-start B.bat:

MongoDB Shell version:2.6.5
Connecting To:127.0.0.1:27017/admin

See this note, MONGO Shell has been started, themiddle of the 2.6.5 6 is even, the representative is a stable release version, odd for the development version

A little bit of simple small command:

To create a database:

> Use Foobar
Switched to DB Foobar

Do nothing at this time or close the window, the database immediately disappears

> Db.persons.insert ({name: "Uspcat"})
Writeresult ({"ninserted": 1})

Insert a record, the persons document will exist in the Foobar database

Commands that show which databases are available:

> Show DBS
Admin (empty)
Foobar 0.078GB
Local 0.078GB

Which collection commands are displayed:

> Show Collections
Persons
System.indexes

To find commands for persons document records:

> Db.persons.find ()
{"_id": ObjectId ("543d357df0b430df52a3ef24"), "name": "Uspcat"}

You can also use FindOne () to find the first record:

> Db.persons.findOne ()
{"_id": ObjectId ("543d357df0b430df52a3ef24"), "name": "Uspcat"}

Insert Record:

> Db.persons.insert ({name: "extjs4.0"})
Writeresult ({"ninserted": 1})
> Db.persons.find ()
{"_id": ObjectId ("543d357df0b430df52a3ef24"), "name": "Uspcat"}
{"_id": ObjectId ("543d370df0b430df52a3ef25"), "name": "extjs4.0"}

Update operation:

> var p = db.persons.findOne ()
> P
{"_id": ObjectId ("543d357df0b430df52a3ef24"), "name": "Uspcat"}
> db.persons.update (p,{name: "Uspcat2"})
Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1})
> var p = db.persons.findOne ()
> P
{"_id": ObjectId ("543d357df0b430df52a3ef24"), "name": "Uspcat2"}

The reason you can declare Var is because the MONGO shell is a JS engine.

The update operation is best done with the Finder and the modifier:

> db.persons.update ({name: "extjs4.1"},{$set: {age:1,name: "Tom2"}});

This changes the name of the second record to Tom2 and adds the Age:1 Bson

Delete operation:

> Db.persons.remove ({name: "Tom2"})
Writeresult ({"nremoved": 1})

CTRL + C exit MONGO Shell engine

CTRL + C exits Mongod server, y command terminates batch processing


MongoDB installation start adding additions and deletions to the document

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.