Mongodb left hand Redis Chapter III mongdb operation

mongodb query operation

1. query operation

db.getCollection ( 'example_data_1') find ({}) to query all the information, corresponding to the mysql select * from table_name.;

2. Query the specific conditions of need in

db.getCollection ( 'example_data_1'). find ({}) in the find ({}) in the set condition the braces

   # Inquiry in the collection example_data_1, the age of information between 21 and 25.

1 db.getCollection('example_data_1').find({'age':{'$lt':25,"$gt":21}})

$ Lt -> less than -> less than

$ Gt -> great than -> greater than 

$ Lte -> less than and equal -> less

$ Gte -> great than and equal -> greater than or equal

$ Ne -> not equal -> not equal

Remember that it is good to remember these English expressions

db.getCollection ( 'example_data_1') find. ({ 'title': "Chapter"}), the query condition type is the setting condition through the dictionary. All information is equal to the query title of the first chapter.

 

3. The field had to be defined in the form:

1 db.getCollection ( " example_data_1 " ) .find (dictionary for filtering recording, dictionary for defining fields), wherein the key for the dictionary defined field name for each field, only two values, 0 or 1.

 

for example:

  db.getCollection('example_data_1').find({})

Query data sets example_data_1, but does not return "address" and "age" field, the query is as follows:

db.getCollection('example_data_1').find({},{‘address’:0,"age":0})

4. to query the data and how many pieces of data

db.getCollection ( 'example_data_1'). find ({}). count () # how many query data obtained

5. Limited return results, limit ()

db.getCollection ( 'example_data_1'). find ({}). limit (limit number of returns) 

db.getCollection('example_data_1').find({}).limit(2)

6 pairs results of a query sort sort ()

1 db.getCollection ( ' example_data_1 ' ) .find ({ ' Age ' : " $ gt " :} 21 is). Sort ({ " Field Name " : -1/1}) where -1 is reverse, 1 denotes positive-sequence .

 

  

7 modify data

update operation, the corresponding command MongoDB updateOne () or the updateMany ()

the difference:

updateOne (): only the update data of the first to meet the requirements.

updateMany (): update all data to meet the requirements.

db.getCollection ( 'example_data_1') updateMany ( . 

 Parameter 1: The first dictionary query statement, 
               
  ( '$ SET': { 'Field 1': "new value 1", "Field 2": "new value 2 "})    

)

  Like updatemany () first argument and find () argument is a dictionary, looking for all records need to be updated.

db.getCollection("example_data_1").updateMany({"name":"张小二"},{"$set":{"work":"工程师"}})

8 delete data

To remove fields from example_date_1 "hello" This is a world record.

8.1 to delete data from a single set of

8.2 Delete multiple data from the collection. As long as you query data, deletes data, in order accidentally deleted data, the general practice is to query the data you want to delete, and then delete the data then isolated.

db.getCollection ( ' example_data_1 ' ) .find ({ " Hello " : " World " }) 
to query the data to be deleted

Then find modified to deleteOne, delete only data to meet the requirements of the first, to find or modify the deleteMany, if you want to delete all the data to meet the requirements.

In the data returned, "acknowledged": true representation deleted successfully, deletedCount: represents a total delete several data.

Query again

no data.

AttentionsPlease

  General engineering uses "false Delete", increase data "deleted" field, if expressed as 0 is not deleted, a value of 1 is deleted.

9 deduplication operation, and use the same keyword mysql DISTINCT ()

db.getCollection("example_data_1").distinct("age")

 

Deduplication data for certain conditions

db.getCollection("example_data_1").distinct("age",{‘age’:{"$gte":10}})

 

Guess you like

Origin www.cnblogs.com/hamish26/p/11356490.html