MongoDB first knowledge-adding and deleting changes

Source: Internet
Author: User

1. Switch database

Admin database: Use admin

Test database: Use test

2. Added:

Method One (preferred)

    c={Name="Junior", Teacher: "Xiao Zhang", stucount:    db.class. Save (c)    or Db.class. Insert (c)

Method Two

Db.class. Insert ({name: "The Junior", Teacher: "Xiao Zhang", stucount:)

Note that saving the same will prompt objectid cannot be repeated

 

3. Delete

Db.class.remove ({name: "the"})

4. Modification

Modify the Teacher field value (only one that satisfies the condition is modified)

Db.class. Update ({name: "},{$set: {name:" Teacher "," Hehhe ", Stucount:]})

Modify all that meet the criteria

Db.class. Update ({name: "},{$set: {name:" Teacher "," tt ", Stucount:}},{multi:true})

Another update is less than the new

Db.class. Update ({name: "},{$set: {name:" Teacher "," Hehhe ", Stucount:}},{upsert:true})

5. Enquiry

Without any conditions

Db.class.find ()

Query the "name=" data

Db.class.find ({name: "the"})

Querying all data for stucount>10

Db.class.find ({"Stucount", {$gt:ten})

Query the first piece of data for stucount<5

Db.class.findOne ({"Stucount": {$lt:5}})

Querying the 10>STUCOUNT>5 data

Db.class.find ({"Stucount": {$lt:ten, $gt:5}})

Paging Query

Db.class.find (). Skip.Limit (5)

How many data are queried

Db.class.find (). Count ()

How many data are queried to satisfy a condition

Db.class.find ({"Stucount", {$lt:ten}}). Count ()

The current number of queries in a paged query

Db.class.find (). Skip ((). Limit (ten). Count (true)

**count () does not write ture, is the total number

To query using cursors:

Traverse all

var cursor = Db.class.find ()  while (cursor. Hasnext ()) Printjson (cursor.  Next())

Use cursor subscript (overflow may occur)

var cursor = Db.class.find () cursor [1]

Using the array subscript

var arr=db.class.find (). ToArray () arr[1]

foreach Output

Db.class.find (). ForEach (Printjson)

It's not written in the back of the set with conditions.

MongoDB first knowledge-adding and deleting changes

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.