Mongodb常用查询Shell

Posted -早起的码农

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mongodb常用查询Shell相关的知识,希望对你有一定的参考价值。

Mongodb常用查询Shell
分组查询:包括过滤,分组,排序等

DBQuery.shellBatchSize = 2000;


db.test_data.aggregate([$group : _id : "$day", num: $sum : 1,$sort:_id:-1])



db.test_data.aggregate( [$match: status:$in: ["Fail","Cancelled"], updateTime:$gt : new Date(ISODate()-1800000), name:$in:["testA","testB"],$group: _id: '$name', data:  $sum: 1 ])

查询中用到的筛选器:

For comparison of different BSON type values, see the specified BSON comparison order.

Name	Description
$eq	Matches values that are equal to a specified value.
$gt	Matches values that are greater than a specified value.
$gte	Matches values that are greater than or equal to a specified value.
$in	Matches any of the values specified in an array.
$lt	Matches values that are less than a specified value.
$lte	Matches values that are less than or equal to a specified value.
$ne	Matches all values that are not equal to a specified value.
$nin	Matches none of the values specified in an array.

Logical
Name	Description
$and	Joins query clauses with a logical AND returns all documents that match the conditions of both clauses.
$not	Inverts the effect of a query expression and returns documents that do not match the query expression.
$nor	Joins query clauses with a logical NOR returns all documents that fail to match both clauses.
$or	Joins query clauses with a logical OR returns all documents that match the conditions of either clause.
Element
Name	Description
$exists	Matches documents that have the specified field.
$type	Selects documents if a field is of the specified type.

Evaluation
Name	Description
$expr	Allows use of aggregation expressions within the query language.
$jsonSchema	Validate documents against the given JSON Schema.
$mod	Performs a modulo operation on the value of a field and selects documents with a specified result.
$regex	Selects documents where values match a specified regular expression.
$text	Performs text search.
$where	Matches documents that satisfy a javascript expression.

Geospatial
Name	Description
$geoIntersects	Selects geometries that intersect with a GeoJSON geometry. The 2dsphere index supports $geoIntersects.
$geoWithin	Selects geometries within a bounding GeoJSON geometry. The 2dsphere and 2d indexes support $geoWithin.
$near	Returns geospatial objects in proximity to a point. Requires a geospatial index. The 2dsphere and 2d indexes support $near.
$nearSphere	Returns geospatial objects in proximity to a point on a sphere. Requires a geospatial index. The 2dsphere and 2d indexes support $nearSphere.

Array
Name	Description
$all	Matches arrays that contain all elements specified in the query.
$elemMatch	Selects documents if element in the array field matches all the specified $elemMatch conditions.
$size	Selects documents if the array field is a specified size.
Bitwise
Name	Description
$bitsAllClear	Matches numeric or binary values in which a set of bit positions all have a value of 0.
$bitsAllSet	Matches numeric or binary values in which a set of bit positions all have a value of 1.
$bitsAnyClear	Matches numeric or binary values in which any bit from a set of bit positions has a value of 0.
$bitsAnySet	Matches numeric or binary values in which any bit from a set of bit positions has a value of 1.

Comments
Name	Description
$comment	Adds a comment to a query predicate.

Projection Operators
Name	Description
$	Projects the first element in an array that matches the query condition.
$elemMatch	Projects the first element in an array that matches the specified $elemMatch condition.
$meta	Projects the document’s score assigned during $text operation.
$slice	Limits the number of elements projected from an array. Supports skip and limit slices.

觉得不错扫头像,关注我的公众号,获取更多大数据技能

以上是关于Mongodb常用查询Shell的主要内容,如果未能解决你的问题,请参考以下文章

02 MongoDB数据类型重要概念以及shell常用指令

MongoDB—— shell常用命令

MongoDB—— shell常用命令

MongoDB shell操作

使用 MongoDB Driver for C# 的不同查询比通过 MongoDB shell 发送的相同查询慢得多

Mongodb:从 mongo shell 中的 ObjectId 执行日期范围查询