Basic use of the shell in MongoDB

Source: Internet
Author: User
Tags findone

1 . Create a database that is named Foobar

Use Foobar

this Foobar the database was not actually created. Just in the MONGO cache.

!--[if GTE VML 1]> !--[If!vml]-->!--[endif]-->

But if you leave directly, the database will be MONGO deleted.

2. Now we Add a collection to the Foobar database and add a record to do something to keep it from being deleted

now it's used. Foobar . This db represents this database .

Db.persons.insert ({name: "ABCdef"})

The above code means to use the Foobar This database, add a Persons collection. added a record to persons. name=abcdef

3. Querying all the databases

Show DBS

The number that follows indicates the size of the database

4. Querying all documents in the current database

Show collections

the first document is persons

the second document is Indexes is automatically generated by the system

View system content in a document

Db.system.iindexes.find ()

Enquiry Persons all the data in the document

Db.persons.find ()

querying the first piece of data : can only query to the first piece of data

Db.persons.findOne () Note findOne O to capitalize

5. Update the contents of the document

put the first record Name=uspcat the record. Change into uspcat1111

Db.persons.update ({name: "Uspcat"},{$set: {name: "uspcat1111"}})

The preceding parameter is the modified condition, and the following parameter is the value to modify. MongoDB defaults to the first record

The re-query discovery record has been changed.

Defining Variables P The value is the first record of the query. then print p

var p =db.persons.findone ()

P

then through the variable P to modify the first record

Db.persons.update (p,{name: "uspcat000"})

Db.perons.findOne ()

adds a field to an existing record. MONGO can arbitrarily increase any type of field in a record

First , modify the value of the first record to age=1

Db.persons.update (P,{age:1})

and then update

Db.persons.update ({age:1},{$set: {name: "Hello.world"}})

The above code means that when the first record age=1 the time. Add Field name=hello.world

6. Delete data from a document

Db.persons.remove ( Modify Condition )

db.persons.remove ({age:2}) Delete age=2 the records

Query again. The record is gone.



From for notes (Wiz)

Basic use of the shell in MongoDB

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.