MongoDB Shell Commands

When operating on data, it should be noted that in non-relational data, different types of data may be stored in the same field, so whether or not quotation marks are added, the query results may be different. In addition, the key value of the condition can be quoted, or the name can be used directly.

1. Create a database: use runoob

2. Simple query (where test1 is the collection table name, and name is the field name):

db.Collection1.find({
    'name':'liu',
    'id':'1'
})

 3. Simple insert:

db.Collection1.insert({
   'name':'gang',
   'id':'2'
})

 4. Simple update (update all fields of the record):

//Note that the update here is equivalent to delete first, then add, that is, there is only one field name left after the update.
//update(query target condition, modified value)
db.Collection1.update({
   'name':'gang',
   'id':'2'
},
{
   'name':'gang'
})

 5. Simple update (update the specified field of the record):

//where $set can be seen as a function
db.Collection1.update(
{  
   'name':'gang'  
},  
{$set:{  
   'name':'gang1'  
}},
false,
true
)  

Statement example: db.table_name.update(where,setNew,issert,multi ); 

where: similar to the query conditions behind the update statement where in sql 

setNew: similar to the part after set in the update statement in sql, which is the part you want to update 

upsert: If the record to be updated is not found, whether to insert a new record, the default is false not to insert, true is to insert 

multi : whether to update multiple records that meet the conditions, false: only the first record is updated, true: multiple records are updated, the default is false 

6. Delete:

db.Collection1.remove({
   'name':'gang1'
})

7. Create a new index:

//Here is the joint index, (1 and -1) means indexing in ascending or descending order
db.Collection1.ensureIndex(
  {
    'id': 1,
    'name': -1
  },
  {background: true}
)

8. Create a unique index:

db.Collection1.ensureIndex(
  {'id':1}
  ,
  {unique:true}
)

 9. Query index:

db.Collection1.getIndexes()

10. Delete the index:

db.Collection1.dropIndex("index_name")

11. Create a new full-text index:

//Create a full-text index for the field content
db.Collection1.ensureIndex({content:'text'})

 12. Query the full-text index:

//Create a full-text index on content
db.Collection1.find({
  $text:{
    $search:"liu"
  }
})

 

expand:

1) Query and: conditions can be separated by commas.

2) Query or:

db.Collection1.find({
  $or:[
  	{'id':'1'},
  	{'id':'2'}
  ]
})

 3) and and or are used together:

db.Collection1.find({
  'id':{$gt:'1'}
  ,
  $or:[
  	{'id':'1'},
  	{'id':'2'}
  ]
})

 4) Conditional judgment (greater than, less than, etc.):

//greater than ($gt), less than ($lt), greater than or equal to ($gte), less than or equal to ($lte)
db.Collection1.find({
   'id':{$gt:1}
})

 5) Return the number of records:

//Get the first 2 records
db.Collection1.find({
  'id':{$gt:'1'}
}).limit(2)

 6) Number of skip records:

//Then skip 1 record first, then take the remaining 2 records
db.Collection1.find({
  'id':{$gt:'1'}
}).limit(2).skip(1)

 7) Sort:

//sort, 1 ascending, -1 descending
db.Collection1.find({
  'id':{$gt:'1'}
}).sort({
  "id":-1
})

 8) Aggregate, mainly used to process data (such as statistical average, summation, etc.), similar to count(*) in SQL statements.

 

//$group is grouped according to name (_id is a fixed value for grouping), and used before grouping, as well as $avg, $min, etc. match is a filter, and multiple conditions are separated by braces {}, such as sorting, etc.
//相当于select name, count(*) from Collection1 group by name
db.Collection1.aggregate([
 {$group : {
    _id : "$name",
    num_tutorial : {$sum : 1}
    first_age : {$first : "$age"}
  }
 },{
    $match: {"pages": {$gte: 5}}
 }
])
 However, what if you don't want to group, and what if you ask for the number of records, solve it in other ways, like:
db.Collection1.find({
    'name':'liu'
}).count()
9) MapReduce: Use MapReduce to implement two functions, Map function and Reduce function. The Map function calls emit(key, value), traverses all the records in the collection, and passes the key and value to the Reduce function for processing. 

 
//Example: Query the matching records through the query condition, and group by the name in the emit to calculate the number of occurrences. find() prints out the data.
db.Collection1.mapReduce(
   function() { emit(this.name,1); },
   function(key, values) {return Array.sum(values)},
      {  
         query:{name:"liu"},  
         out:"post_total"
      }
).find()
 10) Full-text search: It is enabled by default after version 2.6. If there is an article table, the content field can establish a full-text index, so that the full-text content of the article can be searched.
db.Collection1.find({
  $text:{
    $search:"liu"
  }
})
 However, the correct usage format should be (I tested it myself but reported an error, waiting for a solution...):
db.collection.runCommand( "text",{
  search: <string>, //query condition, for example
  filter: <document>, //filter condition, for example filter: { age: { $gt: 10 } }
  project: <document>, //The field to be returned, for example project: { "name": 1 }, 1 is returned
  limit: <number>, // limit the number, for example limit: 2
  language: <string> //Specify language (no Chinese), for example language: "spanish"
})
 In addition, if the query result set is to be returned in order of similarity, you can use:
// Sort by similarity and return the socre field
db.Collection1.find(
  {$text:{$search:"Hahaha"}},
  {score:{$meta:"textScore"}}
).sort({score:{$meta:"textScore"}})
 Condition not:
//The match must have ttt, but cannot contain aaa
db.Collection1.runCommand("text",{search:"ttt -aaa"})
 

Remote knowledge points:

1) Query according to $type (in a non-relational database, the same field, the stored value may be of different types):

 

//{$type:2} represents the string String type
db.Collection1.find({
   'name':{$type:2},
})

2) Replication (that is, synchronizing data), one master and one slave, one master and multiple slaves.

3) Monitoring, you can view the running status.

4) The use of java :

5) Use explain() to analyze the query usage index.

6) Transactions are not supported, but many operations are atomic operations, such as:

 

{ $set : { field : value } }
{ $inc : { field : value } }
 

 

 

 

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326221591&siteId=291194637