MongoDB Database command operation (ii)

Source: Internet
Author: User
Tags findone mongodb

Now learn more about MongoDB's database operations.

Query statements
Db.xxx (collection name). Find () # query Db.xxx (collection name). FindOne () # returns only one Db.xxx (collection name). FindOne (). Pretty () # return result formatted # comparison operator equals, The default is equal to the judgment, there are no operators, such as: Db.xxx (collection name). Find ({name: "Xiao"}) is less than $lt, such as: Db.xxx (collection name). Find ({age:{$lt: 30}}) less than or equal to $lte, such as: Db.xxx (collection name). Find ({age:{$lte: 30}}) is greater than $GT, such as: Db.xxx (collection name). Find ({age:{$gt: 30}}) is greater than or equal to $gte, such as: Db.xxx (collection name). Find ({age:{$gte: 30}}) is not equal to $ne, such as: Db.xxx (collection name). Find ({age:{$ne: 30}) # logical operator, which is the multi-condition query Db.xxx (collection name). Find ({age:{$ne: 30 }, Name: "Xiaoming"}) # Default logic with Db.xxx (collection name). Find ({$or: [{age:{$ne: +}, Name: "Xiaoming"}]}) # Default logical OR # Range query Db.xxx (collection name). Find ({age:{$in: [18,30]}) # Db.xxx (collection name) in a range. Find ({age:{$nin: [18,30]}}) # is not in a range # regular $regexdb.xxx (collection name). Find ({ name:{$regex: "^x"}) # function query $where, use this to represent this collection db.xxx (collection name). Find ({$where: function () {return This.age < 10}}) # Limit the number of returns DB.XXX (collection name). Find (). Limit (3) # query offset db.xxx (collection name). Find (). Skip (3) # returns # loop for (i=0;i<15;i++) {db.xxx starting from 4th) Collection name). Insert ({_id:i*2})}# the query returns some fields that need to return a setting of 1 and do not need to set 0db.xxx (collection name). Find ({},{name:1,age:0}) # Sort (), 1-bit ascending, 1 to descending DB.xxx (collection name). Find (). Sort ({name:-1}) # returns the number of Count () db.xxx (collection name). Count ({name: "Xiaoming"}) # go to Heavy db.xxx (set name). Distinct ("name", {})
Aggregation functions
    • Syntax: DB. Collection name. Aggregate ({pipe: {expression}})
# 管道$group:将集合中的文档分组,可用于统计结果$match:过滤数据,只输出符合条件的文档$project:修改输入文档的结构,如重命名、增加、删除字段、创建计算结果$sort:将输入文档排序后输出$limit:限制聚合管道返回的文档数$skip:跳过指定数量的文档,并返回余下的文档$unwind:将数组类型的字段进行拆分# 表达式$sum:计算总和,$sum:1同count表示计数$avg:计算平均值$min:获取最小值$max:获取最大值$push:在结果文档中插入值到一个数组中$first:根据资源文档的排序获取第一个文档数据$last:根据资源文档的排序获取最后一个文档数据
Index
    • 1 means ascending,-1 means descending

    • Syntax structure: DB. Collection_name.ensureindex (Keys[,options])

keys,要建立索引的参数列表。如:{KEY:1},其中key表示字段名,1表示升序排序,也可使用使用数字-1降序。options,可选参数,表示建立索引的设置。可选值如下:background,Boolean,在后台建立索引,以便建立索引时不阻止其他数据库活动。默认值 false。unique,Boolean,创建唯一索引。默认值 false。name,String,指定索引的名称。如果未指定,MongoDB会生成一个索引字段的名称和排序顺序串联。dropDups,Boolean,创建唯一索引时,如果出现重复删除后续出现的相同索引,只保留第一个。sparse,Boolean,对文档中不存在的字段数据不启用索引。默认值是 false。v,index version,索引的版本号。weights,document,索引权重值,数值在 1 到 99,999 之间,表示该索引相对于其他索引字段的得分权重。
# 创建普通索引db.集合.ensureIndex({属性:1})# 创建唯一索引,实现唯一约束的功能db.集合.ensureIndex({"name":1},{"unique":true})# 创建联合索引db.集合.ensureIndex({name:1,age:1})#查看集合所有索引db.集合.getIndexes()# 删除索引db.集合.dropIndex(‘索引名称‘)db.集合.dropIndexs() # 删除集合所有的索引

MongoDB Database command operation (ii)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.