MongoDB query

 Null data type query:
    -- When performing a query with null data, all documents whose value is null and do not contain the specified key will be retrieved.
    > db.test.find({"x":null})
    { "_id" : ObjectId("4fd59d30b9ac507e96276f1b"), "x" : null }
    { "_id" : ObjectId("4fd59d49b9ac507e96276f1c"), "y" : 1 }
 
    --Need to use null as an element in the array for equality, even if there is only one element in the array.
    --And then there is the use of $exists to determine whether the specified key exists.

    > db.test.find({"x": {"$in": [null], "$exists":true}})
    { "_id" : ObjectId("4fd59d30b9ac507e96276f1b"), "x" : null }

 

Array query, query the movies in China in the country array

 

In the query tags array, at least one element satisfies the movie whose count is greater than 10,000 and whose name is anime

 

When the retrieval array needs to contain multiple elements, use $all here. The order of appearance does not matter

 

The following example indicates exact matching, that is, the retrieved documents, the array data must exactly match the query conditions, that is, neither more nor less, and the order must be consistent.

Matches the value of the specified subscript element in the array. The starting index of the array is 0.

Use $size to get the length of an array, but $size cannot be used in conjunction with comparison operators. If you need to find the length of the array within a certain range, you can add a field or use the function

Using forEach takes a long time

 

Return part of the data in the array via $slice. "$slice": 2 means the first two elements in the array.

Return part of the data in the array via $slice. "$slice":-2 means the last two elements in the array.

$slice : [2,1], which means to take 1 from the second 2 elements, if the number of obtained elements is greater than the number of elements after 2, then take all the following data

 

 

Nested query, and the return field is the specified field, the field specified to be displayed or not displayed, and 1 and 0 cannot be mixed

 

When the embedded document is an array, the $elemMatch operator is needed to help locate a certain element matching, otherwise the embedded document will perform all matching.
That is, when searching, all elements need to be listed as query conditions.

    > db.test.findOne()
    {
         "_id" : ObjectId("4fd5af76b9ac507e96276f23"),
         "comments" : [
                 {
                         "author" : "joe",
                         "score" : 3
                 },
                 {
                         "author" : "mary",
                         "score" : 6
                 }
         ]
    }
    > db.test.find({"comments": {"$elemMatch": {"author":"joe","score":{"$gte":3}}}}
    { "_id" : ObjectId("4fd5af76b9ac507e96276f23"),"comments" : [ { "author" : "joe", "score" : 3 }, { "author" : "mary", "score" : 6 } ] }
 

 

 

 

 

Find high scoring movies

 

 

Count the number of movies in each segment and rename the field

 

Find the highest and lowest scores

 

 

Type conversion, NumberLong has requirements for string format, no decimal point

 

mapReduce

db.collection.mapReduce(
   function() {emit(key,value);},  //map 函数
   function(key,values) {return reduceFunction},   //reduce 函数
   {
      out: collection,
      query: document,
      sort: document,
      limit: number
   }
)

Using 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.

The Map function must call emit(key, value) to return a key-value pair.

Parameter Description:

  • map  : The mapping function (generates a sequence of key-value pairs, as a reduce function parameter).
  • The reduce  statistical function, the task of the reduce function is to turn key-values ​​into key-values, that is, to turn the values ​​array into a single value value. .
  • The out  statistics result is stored in the collection (if not specified, a temporary collection is used, which is automatically deleted after the client disconnects).
  • query  a filter condition, only the documents that satisfy the condition will call the map function. (query, limit, sort can be combined at will)
  • The sort sorting parameter combined with sort  and limit (also sorts the documents before sending to the map function) can optimize the grouping mechanism
  • limit  The upper limit of the number of documents sent to the map function (if there is no limit, the use of sort alone is not very useful)

Calculate the number of movies with more than 8 points, save them in the tmp table, and view the results

This method is much slower than using group. . .

            (1) MapReduce uses custom JavaScript functions to perform map and reduce operations, so it is based on js engine, single-threaded execution, inefficient, more complex than Aggregation, and suitable for background statistics.
            (2) MapReduce supports sharding operations, which can be split and distributed to different machines for execution (multi-server parallel processing of data sets), and then the results of processing by different machines are aggregated to output the results.
            (3) MapReduce can perform all operations count, distinct, and group of a single aggregation. However, when the amount of data is very large, the processing capacity of group is not very good. It is filtered first and then grouped. Fragmentation is not supported, which affects the amount of data. limited and inefficient.

 

Single keyword fuzzy query, using regular expression

 

 

The query results are processed using the forEach function, which is more flexible

Use aggregate functions to achieve a similar effect, and it's faster

 

 

Modifying the field type takes a long time to modify the entire document.

 

You can also use update, which will slightly improve the efficiency

Guess you like

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