MongoDB Query-Notes

Source: Internet
Author: User
Tags mongodb query mongodb update

# MONGODB Query Statement

MongoDB: Distributed File Store database, between relational database and non-relational database, Concept: collection (table), document (row), Field (column)

tychtml Database:
# 1. Number of document queries

? Db.getcollection (' companys '). Find ({}). Count ()

# # 2, find ()

Db.getcollection (' companys '). Find ({"_id": ObjectId ("59c12543e1382308c4e0f786")})

? = select * from companys where _id= "59c12542e1382308c4e0f262"

Db.getcollection (' companys '). Find ({},{"summary": 1})

? =select Summary from Companys

Inline document query:
Db.getcollection (' companys '). Find ({"Summary.reportcount": 4}). Limit (1)
Db.getcollection (' companys '). Find ({"Staffcount": {$elemMatch: {"position": "Supervisor", "name": "Song Jialin"}}}). Limit (1)
Db.getcollection (' companys '). Find ({"Summary.reportcount": 4, "Summary.checkcount": 1}). Limit (1)
MongoDB provides a set of comparison operators:

| $lt | < |
| :--: | :--: |
| $lte | <= |
| $GT | > |
| $gte | >= |
| $ne | != |

Delete database: Db.dropdatabase ()

View all databases: Show DBS

Create DATABASE: Use Runoob

Insert Document: DB. Collection_name (document)

Update document: Db.collection.update ()

Querying using the $elemmatch operator

Fuzzy Query $regex

MongoDB Update has two commands: Update\save
Data Update operators:
$inc:
Usage: {$inc: {Field:value}}
Function: Adds value to a field of a number
Example: {"_id": ObjectId ("5030f7ac721e16c4ab180cdb"), "name": "Chenzhou", "Age": 22}
Increase the age of 5 for students with name Chenzhou
Db.students.update ({"Name": "Chenzhou"},{$inc: {"Age": 5}})
$set:
Usage: {$set: {Field:value}}
Function: Sets the value of a field in a document
$unset:
Usage: {$unset: {field:1}}
Role: Delete a field
Cases:
Db.students.update ({"Name": "Chenzhou"},{$unset: {"Age": 1}})
$push:
Usage: {$push: {Field:value}}
Function: Append value To field, (field can only be an array type, if field does not exist, an array type is automatically inserted)
$PUSHALL:
Usage: {$pushAll: {Field:value_array}}
Function: Use same as $push, just $pushall can append multiple values to an array field at a time
$addToSet:
Usage: {$addToSet: {Field:value}}
Function: Adds a value to the array, and only increases if the value does not exist in the array.
$pop:
Usage: Delete the first value in the array: {$pop: {field:-1}}, delete the last value in the array: {$pop: {field:1}}
Function: Used to delete a value within an array
$pull:
Usage: {$pull: {Field:_value}}
Function: Removes a value equal to _value from the array field
$PULLALL:
Usage: {$pullAll: Value_array}
Function: Use same as $pull, you can delete multiple values in an array at once
$rename:
Usage: {$rename: {old_field_name:new_field_name}}
Role: Renaming a field

MongoDB Query-Notes

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.