MongoDB (iv)

Source: Internet
Author: User
Tags prev

Count+distinct+group
Database command Actions
Fixed set properties
Gridfs File System
Supplementing server-side scripting

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}}}) 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; }}, Finalize:function(prev) {PREV.M= prev.name+ "Math scores" +prev.m}, condition:{m:{$gt:90}}}) Db.persons.insert ({name:"Uspcat", Age:27, Email:"[Email protected]", C:89,m:100,e:67, Country:"China", books:["JS", "JAVA", "ExtJS", "MONGODB"]}) 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}}}})

1.Count
Please inquire about the number of American students in persons.
Db.persons.find ({ Country: "USA"}). Count ()
2.Distinct
Please find out how many countries are in persons.
Db.runcommand ({distinct: "Persons", Key: "Country"}). Values
3.Group
Syntax:
Db.runcommand ({group:{
ns: Collection name,
Key: Grouped Key object,
Initial: Initialize accumulator,
$reduce: Group decomposition,
Condition: Condition,
Finalize: Group Completion
}})
grouping is first grouped by key, each document of each group is executed $reduce method,
He receives 2 parameters one is the record in the group, One is the accumulator data.
3.1 Please find out the best student information for each country in persons (must be above 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: +}}}})

3.2 On the basis of the 3.1 requirement. No personal information to link up write a description assignment to M on
Finalize:function (prev) {
PREV.M = prev.name+ "Math Scores "+PREV.M
}
4. To format a grouped key with a function
4.1 If the key counrty and country are present in the collection The grouping is a bit of a hassle.
$keyf: Function (doc) {
return {country:doc.counTry}
},.....


1. Command Actuator RunCommand
1.1 Performing a delete table operation with a command
Db.runcommand ({drop: "map"})
{
"Nindexeswas": 2,
"MSG": "Indexes dropped for collection",
"NS": "Foobar.map",
"OK": 1
}
2. How to query the commands that MongoDB provides US
1. Execute db.listcommands () in the shell
2. Visit URL http://localhost:28017/_commands
3. Examples of common commands
3.1 Querying server version numbers and host operating systems
Db.runcommand ({buildinfo:1})
3.2 Query Execution collection details, size, space, index, etc. ..
Db.runcommand ({collstats: "Persons"})
3.3 Viewing operations this collection last error message
Db.runcommand ({GetLastError: "Persons"})

1. Fixed set concept


2. Fixed characteristics
2.1 Fixed Set default is no index, even _id is not indexed
2.2 Because there is no need to allocate new space, his insertion speed is very fast.
2.3 The fixed set of shun is determined to cause the query speed is very fast
2.4 The most suitable is the application is log management
3. Create a fixed collection
3.1 Creating a new fixed collection requires a size of 100 bytes that can store 10 documents
Db.createcollection ("Mycoll", {size:100,capped:true,max:10})
3.2 Convert a normal set to a fixed set
Db.runcommand ({converttocapped: "Persons", size:100000})
4. Reverse sort, default is Insert order.
4.1 Query fixed set mycoll and response sort
Db.mycoll.find (). Sort ({$natural:-1})
5. Tail cursor, unfortunately the shell does not support Java and PHP drivers are supported
5.1 tail cursor Concept
This is a special cursor that can only be used at fixed levels and bodies, and he has no results.
Nor automatic destruction. He's waiting for the results to come.
1. Concept
Gridfs is a MongoDB-brought file system he stores files in binary form
The majority of large file systems are feature Gridfs can be completed completely
2. Tools to use

Mongofiles.exe
3. Using Gridfs
3.1 View all features of Gridfs
Cmd?mongofiles
3.2 Uploading a file
mongofiles-d foobar-l "E:\a.txt" put "a.txt"
3.3 Viewing the Gridfs file storage status
Use Vue to view


Collection View
Db.fs.chunks.find () and Db.fs.files.find () store all file information for the file system
3.4 Viewing the contents of a file
c:\users\thinkpad>mongofiles-d foobar get "a.txt"
Vue can see that the shell cannot open the file
3.5 View All Files
mongofiles-d Foobar List
3.5 Delete an existing file in the Vue operation
mongofiles-d foobar Delete ' a.txt '


1.Eval
1.1 Server-side run eval
Db.eval ("function (name) {return name}", "Uspcat")
2.Javascript of storage
2.1 Save JS variable on service live function common Global call
1. Load variables into special set System.js
Db.system.js.insert ({_id:name,value: "Uspcat"})
2. Call
Db.eval ("return name;")
System.js is equivalent to a stored procedure in Oracle because value can not only write variables
You can also write the function body, which is the JavaScript code

MongoDB (iv)

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.