MongoDB Database Document Operations

Source: Internet
Author: User
Tags findone logical operators mongodb collection mongodb limit throw exception

Previous words

This article will detail the MongoDB database on the deletion and modification of documents

Insert Document

To insert data into a MongoDB collection, you need to use MongoDB insert() or save() methods, as well as the Insertone () or Insertmany () method

"Insert ()"

  insert()The basic syntax of the command is as follows

Db. Collection_name.insert (document)

In the inserted document, if you do not specify _id a parameter, MongoDB assigns a unique objectid to the document. The _id hexadecimal number of bytes unique to each document in the collection 12

If a collection does not exist in the database, MongoDB creates this collection and then inserts the document into the collection

To insert multiple documents in a single query, you can insert() pass an array of documents in a command

You can use the JS syntax to insert multiple documents

"Save ()"

Inserting a document can also be used db.post.save(document) . If it is not specified in the document _id , the save() method insert() automatically assigns the value of the ID as the method does. If specified _id , save() all data for the contained document is replaced as a method _id .

The difference between the Save () method and the Insert () method is that the save () method can be overwritten or modified, and the Insert () method cannot

Db.post.save (document)

"Insertone ()"

Use the db.collection.insertOne() method to insert a single document into the collection

"Insertmany ()"

Use the db.collection.insertMany() method to insert multiple documents into the collection

Querying documents

"Find ()"

To query the data from the MongoDB collection, you need to use MongoDB's find() method, by default return the first article in the result, and enter "it" to display the next 20 documents.

  find()The basic syntax of the command is as follows:

Db. Collection_name.find (document)

  find()Method will display all documents in an unstructured manner

Can qualify query criteria

The returned key can be specified by the second parameter of find, a value of 1 or True indicates that the key is displayed, and a value of 0 or false means that the key is not displayed

The count () method under the Find () method can display the number of documents that meet the criteria

"FindOne ()"

  findOne()method returns only one document, the document that was first added

"Comparison operator"

Less than  {<key>:{$lt:<value>}} is    less than or equal to    {<key>:{$lte:<value>}}    Greater than  {  <key>:{$gt:<value>}} is    greater than or equal to    {<key>:{$gte:<value>}}      Not equal    to {<key>:{$ne: <value>}}  
equals {<key>:{$eq: <value>}}

Gets the value of x less than 2

Get a value of x greater than or equal to 2

Gets the value x is not equal to 2

"Logical Operator"

You can use logical operators $and, $or to represent the

{$and: [{<expression1>}, {<expression2>}, ..., {<expressionN><expression1>}, { <expression2>}, ... {<expressionN> }]}

"Regular expression"

A document query can use regular expressions, but only data of string types is supported

"$where"

The $where operator is powerful and flexible, it can use arbitrary javascript as part of a query, a string containing JavaScript expressions, or a JavaScript function

Using strings

Using functions

Limit and Skip

"Limit ()"

If you need to read a specified number of data records in MongoDB, you can use the MongoDB limit method, and the limit () method accepts a numeric parameter that specifies the number of record bars read from MongoDB

Default returns the first article in the results, enter "It" to display the next 20 documents

Displays all data in the collection if the parameter in the limit () method is not specified

Db. Collection_name.find (). Limit (number)

"Skip ()"

You can use the Skip () method to skip a specified number of data, and the Skip method also accepts a numeric parameter as the number of skipped record bars

Db. Collection_name.find (). Skip (number)

Sort

"Sort ()"

Using the sort () method in MongoDB to sort the data, the sort () method can specify the sorted field by parameter, and use 1 and-one to specify how the sort is sorted, where 1 is in ascending order, and 1 is used for descending order

Db. Collection_name.find (). Sort ({key:1})

Update document

MongoDB uses the update () or save () method to update the document in the collection

"Update ()"

The update () method is used to update a document that already exists. The syntax format is as follows:

db.collection.update (<query>,<update>,{upsert:<boolean;, Multi: <  Boolean>,writeconcern:<document>})
The query:update query condition, similar to the Update:update object in the Where in the SQL update query and some updated operators such as $, $inc ... ), and so on, can also be understood as SQL update query within the set after the Upsert: optional, this parameter means that if there is no record of the update, whether the insert Objnew,true is inserted, the default is False, do not insert multi: optional, MongoDB The default is False, update only the first record found, if this parameter is true, it will be found on the condition of a number of records update Writeconcern: optional, throw exception level

[note] After testing, the Upsert parameter cannot be set to TRUE or FALSE, you can insert a new field

MongoDB defaults to update only the first record found, x:1, update to X:10

It is important to note that if you do not use $set, replace the contents of the document with X:10

Update all records, X:10, update to X:1

MongoDB default only adds the first record found to the update, x:1 the record, adds Z:1

Will find all the records of the X:2, add z:2

"Save ()"

The Save () method can insert or update the document, insert if the _id of the document in the parameter differs from the _id that exist in the collection, or, if the same, update

Delete a document

The MongoDB remove () function is used to remove data from the collection

"Remove ()"

By default, MongoDB deletes all documents that match the criteria

Db.collection.remove (<query>,{justone: <boolean;, Writeconcern: <document>})
true or 1, only one document is deleted. Writeconcern: (optional) the level at which the exception is thrown. 

Delete only the first document that matches a condition

Delete all documents that match the criteria

MongoDB Database Document Operations

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.