MongoDB lecture 5

Source: Internet
Author: User
CountDistinctGroup1.Count count the number of American students in persons. db. persons. find ({country: USA }). count () 2. in Distinct, check the total number of countries in persons. db. runCommand ({distinct: persons ", key: country }). values3.Group Syntax: db. runCommand ({g

CountDistinctGroup 1. count please query the number of American students in persons. db. persons. find ({country: USA }). count () 2. in Distinct, check the total number of countries in persons. db. runCommand ({distinct: persons ", key: country }). values 3. group Syntax: db. runCommand ({g

Count + Distinct + Group

1. Count

Query the number of American students in persons.

Db. persons. find ({country: "USA"}). count ()

2. Distinct

Check the total number of countries in persons.

Db. runCommand ({distinct: "persons", key: "country"}). values

3. Group

Syntax:

Db. runCommand ({group :{

Ns: Set Name,

Key: The Key object of the group,

Initial: Initialize the accumulators,

$ Reduce: group splitter,

Condition: Condition,

Finalize: group completor

}})

Groups are first grouped by key. Each document in each group needs to execute the $ reduce method,

He receives two parameters. One is the record in the group and the other is the data of the accumulators.

3.1 Please find out the student information with the best math scores for each country in persons (which must be over 90)

Db. runCommand ({group :{

Ns: "persons ",

Key: {"country": true },

Initial: {m: 0 },

$ Reduce: function (doc, prev ){

If (doc. m> prev. m ){

Prev. m = doc. m;

Prev. name = doc. name;

Prev. country = doc. country;

}

},

Condition: {m :{$ gt: 90 }}

}})

3.2 Based on the 3.1 requirement, each person's information is linked up and a description is assigned to m.

Finalize: function (prev ){

Prev. m = prev. name + "Math scores" + prev. m

}

4. format the group key using the function

4.1 if both the Counrty and counTry keys exist in the collection

Db. persons. insert ({
Name: "USPCAT ",
Age: 27,
Email: 2145567457@qq.com ",
C: 89, m: 100, e: 67,
CounTry: "China ",
Books: ["JS", "JAVA", "EXTJS", "MONGODB"]
})

So grouping is a little troublesome. How can this problem be solved?

Db. runCommand ({group :{
Ns: "persons ",
$ Keyf: function (doc ){
If (doc. counTry ){
Return {country: doc. counTry}
} Else {
Return {country: doc. country}
}
},
Initial: {m: 0 },
$ Reduce: function (doc, prev ){
If (doc. m> prev. m ){
Prev. m = doc. m;
Prev. name = doc. name;
If (doc. country ){
Prev. country = doc. country;
} Else {
Prev. country = doc. counTry;
}
}
},
Finalize: function (prev ){
Prev. m = prev. name + "Math scores" + prev. m
},
Condition: {m :{$ gt: 90 }}
}})

DATABASE Command operations

1. Command executor runCommand

1.1 run a command to delete a table

Db. runCommand ({drop: "map "})

{

"NIndexesWas": 2,

"Msg": "indexes dropped forcollection ",

"Ns": "foobar. map ",

"OK": 1

}

2. How to query the commands provided by mongoDB for us

1. Execute db. listCommands () in shell ()

2. Visit the URL http: // localhost: 28017/_ commands. You must first pass

Mongod -- dbpath d: \ app \ mongodata -- rest command to open a simple rest API

3. Examples of Common commands

3.1 query server version numbers and host operating systems

Db. runCommand ({buildInfo: 1 })

3.2 query the detailed information, size, space, index, etc. of the execution set ......

Db. runCommand ({collStats: "persons "})

3.3 view the last error message of the operation set

Db. runCommand ({getLastError: "persons "})

Fixed set features

2. Fixed features

2.1 By default, a fixed set is not indexed, even if _ id is not indexed.

2.2 because no new space needs to be allocated, the insertion speed is very fast.

2.3 The order of a fixed set is determined, resulting in a very fast query speed.

2.4 The most suitable application is log management. <喎?http: www.2cto.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + signature + 6z9Kqx/Signature + signature + NC63tM/Signature + 5 zLaovK + signature/Signature + signature + NS7OsrK/086x6iy/yc + signature/Signature + signature/q72Sy2 + signature + z7XNs8v708O2/Signature vcd4kpha + ICAgICAgICAgILTz0M3OxLz + signature/Signature + Mi7A + signature + Signature + signature + y6b + signature/Signature + signature + 0zsS8/sTayN08L3A + signature/ydLUsum/Signature + qzsS8/Signature vcd4kpha + My41sum/Signature + signature + My41yb6z/Signature + Signature = "a.txt'

Server scripts

1. Eval

1.1 run eval on the server

Db. eval ("function (name) {return name}", "uspcat ")

2. Javascript Storage

2.1 save js variable living functions on the service for global calls

1. load variables into a special set of system. js

Db. system. js. insert ({_ id: name, value: "uspcat "})

2. Call

Db. eval ("return name ;")

System. js is equivalent to the stored procedure in Oracle, because the value can not only write Variables

You can also write the function body, that is, javascript code.

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.