mongo 操作符

1 $unset  

The $unset operator deletes a particular field。

https://docs.mongodb.com/manual/reference/operator/update/unset/

2 $in

The $in operator selects the documents where the value of a field equals any value in the specified array。

https://docs.mongodb.com/manual/reference/operator/query/in/

3 $nin

Syntax: { field: { $nin: [ <value1>, <value2> ... <valueN> ]} }

$nin selects the documents where:

the field value is not in the specified array or
the field does not exist.

https://docs.mongodb.com/manual/reference/operator/query/nin/

3 $slice 

The $slice operator controls the number of items of an array that a query returns.

https://docs.mongodb.com/manual/reference/operator/projection/slice/

4 $all

The $all operator selects the documents where the value of a field is an array that contains all the specified elements.

https://docs.mongodb.com/manual/reference/operator/query/all/

5 $and

Syntax: { $and: [ { <expression1> }, { <expression2> } , ... , {<expressionN> } ] }

db.inventory.find( { $and: [ { price: { $ne: 1.99 } }, { price: { $exists: true } } ] } )

$and performs a logical AND operation on an array of two or more expressions (e.g. <expression1><expression2>, etc.) and selects the documents that satisfy all the expressions in the array.

https://docs.mongodb.com/manual/reference/operator/query/and/

6 $match

Filters the documents to pass only the documents that match the specified condition(s) to the next pipeline stage.

The $match stage has the following prototype form:

{ $match: { <query> } }
db.articles.aggregate(
    [ { $match : { author : "dave" } } ]
);

7 $group

https://docs.mongodb.com/manual/reference/operator/aggregation/group/

示例:

  

for i in col.find().limit(5):
    print(i)
{'_id': ObjectId('5698f524a98063dbe9e91ca8'), 'pub_date': '2016-01-12', 'look': '-', 'area': '朝阳', 'title': '【图】95成新小冰柜转让 - 朝阳高碑店二手家电 - 北京58同城', 'url': 'http://bj.58.com/jiadian/24541664530488x.shtml', 'cates': ['北京58同城', '北京二手市场', '北京二手家电', '北京二手冰柜'], 'price': '450 元'}
{'_id': ObjectId('5698f525a98063dbe4e91ca8'), 'pub_date': '2016-01-14', 'look': '-', 'area': '朝阳', 'title': '【图】洗衣机,小冰箱,小冰柜,冷饮机 - 朝阳定福庄二手家电 - 北京58同城', 'url': 'http://bj.58.com/jiadian/24349380911041x.shtml', 'cates': ['北京58同城', '北京二手市场', '北京二手家电', '北京二手洗衣机'], 'price': '1500 元'}
{'_id': ObjectId('5698f525a98063dbe7e91ca9'), 'pub_date': '2015-12-27', 'look': '9成新', 'area': '朝阳', 'title': '台式主机转让 - 朝阳孙河台式机/配件 - 北京58同城', 'url': 'http://bj.58.com/diannao/24440805710645x.shtml', 'cates': ['北京58同城', '北京二手市场', '北京二手台式机/配件', '北京二手台式机'], 'price': '900'}

  1 

pipeline1 = [
    {'$match':{'$and':[{'pub_date':{'$gt':'2016-01-02','$lt':'2016-01-15'}},{'area':{'$all':['昌平']}}]}},
    {'$group':{'_id':{'$slice':['$cates',2,1]},'count':{'$sum':1}}},
    {'$sort':{'count':-1}},
    {'$limit':5},
]

  2

pipeline2 = [
    {'$match':{'$and':[{'pub_date':{'$gt':'2016-01-02','$lt':'2016-01-15'}},{'look':{'$nin':['-']}}]}},
    {'$group':{'_id':'$look','avg_price':{'$max':'$price'}}},
    {'$sort':{'avg_price':-1}},
    {'$limit':5},
]
原文地址:https://www.cnblogs.com/654321cc/p/8795980.html