The polymerization operation mongo

 

 

Data set shown in FIG. 7-1 exampledata1, polymerization operation using the following functions:

(1) does not return _id field returns only age and sex fields.

(2) All records age greater than 28, and returns only age and sex.

(3) In the field $ match returned to add a new field "hello", the value "world".

(4) In the field $ match is returned, add a new field "hello", copy the value of the value of age.

(5) In the field $ match is returned, the value of the age to modify a fixed string.

(6) and the user.name user.user_id become common fields and returns.

(7) in the returned data, add a field "hello", the value "$ normalstring", add a field "abcd", a value of 1.

"$ Match" can filter out records need, if you want to return only part of the field, how they should do it? Then you need to use the keyword "$ project".

Some of the fields returned

First with "$ project" to implement a feature already have - return only part of the field. Format is as follows:

  1. collection.aggregate([{'$project': {字段过滤语句}}])

Here filtering statement field and "find ()" identical to the second parameter, also a dictionary. Field name Key, Value of 1 or 0 (1 Field Value is required, unneeded fields Value is 0).

For example, for the data set shown in FIGS. 7-1, no return "_id" field, returns only age and sex field, the polymerization statement is as follows:

  1. db.getCollection('example_data_1').aggregate([

  2. {'$project': {'_id': 0, 'sex': 1, 'age': 1}}

  3. ]) 

Results shown in FIG 7-22.

 

 

Combined with "$ match" to achieve "first screening records, and then filter field." For example, select all records greater than age 28, and returns only Sex age, the polymerization statement written as:

  1. db.getCollection('example_data_1').aggregate([

  2. {'$match': {'age': {'$gt': 28}}},

  3. {'$project': {'_id': 0, 'sex': 1, 'age': 1}}

  4. ]) 

Results shown in FIG 7-23.

 

 

So far, the use of "$ match" add "$ project", knock dozens keyboard, finally achieved "find ()" function. Polymerization complicated and cumbersome operation, exactly what are the benefits?

Add new field

Add fixed text

Add a field that does not exist in the "$ project" Value of a dictionary, look at the effect of what happens. E.g:

  1. db.getCollection('example_data_1').aggregate([

  2. {'$match': {'age': {'$gt': 28}}},

  3. {'$project': {'_id': 0, 'sex': 1, 'age': 1, 'hello': 'world'}}

  4. ])

Note that the field name "hello", exampledata1 dataset is not the field, but its value is not "0" or "1", but a string.

查询结果如图7-24所示。在查询的结果中直接增加了一个新的字段。

 

 

复制现有字段。

现在把上面代码中的“world”修改为“$age”,变为:

  1. db.getCollection('example_data_1').aggregate([

  2. {'$match': {'age': {'$gt': 28}}},

  3. {'$project': {'_id': 0, 'sex': 1, 'age': 1, 'hello': '$age'}}

  4. ])

查询结果如图7-25所示。

 

 

修改现有字段的数据

接下来,把原有的age的值“1”改为其他数据,代码变为:

  1. db.getCollection('example_data_1').aggregate([

  2. {'$match': {'age': {'$gt': 28}}},

  3. {'$project': {'_id': 0, 'sex': 1, 'age': "this is age"}}

  4. ])

查询结果如图7-26所示。

 

 

从图7-25和图7-26可以看出,在“$project”中,如果一个字段的值不是“0”或“1”,而是一个普通的字符串,那么最后的结果就是直接输出这个普通字符串,无论数据集中原本是否有这个字段。

从图7-26可以看出,如果一个字段后面的值是“$+一个已有字段的名字”(例如“$age”),那么这个字段就会把“$”标记的字段的内容逐行复制过来。这个复制功能初看起来似乎没有什么用,原样复制能干什么?那么现在来看看exampledata2的嵌套字段。

抽取嵌套字段

对于如下图所示的数据集 example_data_2:

 

 

如果直接使用find(),想返回“user_id”和“name”,则查询语句为:

  1. db.getCollection('example_data_2').find({}, {'user.name': 1, 'user.user_id': 1})

查询结果如图7-27所示。

 

 

返回的结果仍然是嵌套字段,这样处理起来非常不方便。而如果使用“$project”,则可以把嵌套字段中的内容“抽取”出来,变成普通字段,具体代码如下:

  1. db.getCollection('example_data_2').aggregate([

  2. {'$project': {'name': '$user.name', 'user_id': '$user.user_id'}}

  3. ])

查询结果如图7-28所示。

 

 

普通字段处理起来显然是要比嵌套字段方便不少,这就是“复制字段”的妙用。

处理字段特殊值

看到这里,可能有读者要问:

如果想添加一个字段,但是这个字段的值就是数字“1”会怎么样?

如果添加一个字段,这个字段的值就是一个普通的字符串,但不巧正好以“$”开头,又会怎么样呢?

下面这段代码是图7-1所示的数据集的查询结果。

  1. db.getCollection('example_data_1').aggregate([

  2. {'$match': {'age': {'$gt': 28}}},

  3. {'$project': {'_id': 0, 'id': 1, 'hello': '$normalstring', 'abcd': 1}}

  4. ])

查询结果如图7-29所示。

 

 

由于特殊字段的值和“$project”的自身语法冲突了,导致所有以“$”开头的普通字符串和数字都不能添加。要解决这个问题,就需要使用另一个关键字“$literal”,代码如下:

  1. db.getCollection('example_data_1').aggregate([

  2. {'$match': {'age': {'$gt': 28}}},

  3. {'$project': {'_id': 0, 'id': 1, 'hello': {'$literal': '$normalstring'}, 'abcd': {'$literal': 1}}}

  4. ])

查询结果如图7-30所示。

 

 

 

转自:公众号 kingname 未闻Code

Guess you like

Origin www.cnblogs.com/tjp40922/p/12199421.html