MongoDB (2)

Source: Internet
Author: User
Tags ming mongodb rand

Mongodb

PS a lot of MongoDB are not set a password, the default MongoDB is not set the password, halo

$type operator

The query type uses the
Gets the title value of the Col collection as a string type document

db.col.find(title: {$type: 2});
The limit and skip method limit queries the specified method
> db.ming.find().pretty();{ "_id" : ObjectId("5b5f6eb7d595bb04cbe14b96"), "ming" : "sdfrf" }{ "_id" : ObjectId("5b5f6f0ad595bb04cbe14b97"), "name" : "refgerfg" }{ "_id" : ObjectId("5b5f6f40d595bb04cbe14b98"), "x" : 222, "y" : 44444 }{ "_id" : ObjectId("5b5f6f50d595bb04cbe14b99"), "x" : 4444, "y" : 22222 }> db.ming.find().limit(2);{ "_id" : ObjectId("5b5f6eb7d595bb04cbe14b96"), "ming" : "sdfrf" }{ "_id" : ObjectId("5b5f6f0ad595bb04cbe14b97"), "name" : "refgerfg" }
Skip is skipping the first few data sorts
> db.ming.find({}, {ming:1, name:1}).sort({_id:-1});{ "_id" : ObjectId("5b5f6f50d595bb04cbe14b99") }{ "_id" : ObjectId("5b5f6f40d595bb04cbe14b98") }{ "_id" : ObjectId("5b5f6f0ad595bb04cbe14b97"), "name" : "refgerfg" }{ "_id" : ObjectId("5b5f6eb7d595bb04cbe14b96"), "ming" : "sdfrf" }> db.ming.find({}.{ming:1, name:1}).sort({id:1});2018-07-31T19:49:11.007+0800 E QUERY    [js] SyntaxError: missing name after . operator @(shell):1:16> db.ming.find({}, {ming:1, name:1, x:1, y:1}).sort({ _id:1});{ "_id" : ObjectId("5b5f6eb7d595bb04cbe14b96"), "ming" : "sdfrf" }{ "_id" : ObjectId("5b5f6f0ad595bb04cbe14b97"), "name" : "refgerfg" }{ "_id" : ObjectId("5b5f6f40d595bb04cbe14b98"), "x" : 222, "y" : 44444 }{ "_id" : ObjectId("5b5f6f50d595bb04cbe14b99"), "x" : 4444, "y" : 22222 }>
Index
> db.ming.createIndex({ming:1});{        "createdCollectionAutomatically" : false,        "numIndexesBefore" : 1,        "numIndexesAfter" : 2,        "ok" : 1}>

The Ming field creates a sequence in ascending order

Polymerization

Count () similar to SQL

db.ming.aggregate(        [            (                $groupp: {                    _id : "$by-user",                    num_tutorial: {                        $sum: 1                    }                }            )        ]    )

Groups are grouped by _id, and the grouped documents are counted, that is, the sum of the grouped documents.

Pipeline
> db.ming.aggregate(... {... $project : {... _id:0,... x:1,... y:1... }... }... ){  }{  }{ "x" : 222, "y" : 44444 }{ "x" : 4444, "y" : 22222 }

Once a value is processed, it is passed to the next processing
That is, when the value passes through the pipeline, it becomes the appropriate format

Get documents randomly
db.ming.aggregate(        [            {                $sample: {                    size:1                }            }        ]    )

Use aggregation to achieve the purpose with $sample, where size is the number of randomly fetched bars

SQL Random fetch two records
select * from dataorder by rand()limit 2
mysql> select * from c order by rand() limit 2;+--------+------------------------+-------+| cno    | cn                     | hourc |+--------+------------------------+-------+| 050192 | 专业英语               | 32    || 090112 |  | 48    |+--------+------------------------+-------+2 rows in set (0.10 sec)mysql> select * from c order by rand() limit 2;+--------+----------------------+-------+| cno    | cn                   | hourc |+--------+----------------------+-------+| 050198 | 计算机网络安全与应用 | 32    || 050150 | 计算机组装与维护     | 36    |+--------+----------------------+-------+2 rows in set (0.03 sec)mysql>

The rest of the copy, can not see anymore.

MongoDB (2)

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.