MongoDB Learning Notes

Source: Internet
Author: User

Mongodb Learning

Installation-level configuration Windows random boot

1. Download MongoDB and unzip to D:\database\mongodb\mongo

2. Configure environment Variables

1. new environment variable mongo_home=d:\database\mongodb\mongo\bin

2. Add the path variable %mongo_home%;

3. Configure the database installation and log folders

1. Add the logs and data folders under the D:\database\mongodb folder

2. Add mongodb.log files under the logs folder

4. Configuring Windows boot- up

Enter mongod--dbpath D:\database\mongodb\data--logpath=d:\database\mongodb\logs\mongodb.log--install on the command line

5. test whether the installation is successful

Enter MONGO on the command line to indicate that the installation was successful

Basic operations

Add Action Db.person.insert ({"Name": "Tom", "Age": +})

Find operations

Find all db.person.find ()

Conditional Query db.person.find ({"Name": "Tom"})

>,>=,<,<=,!=,= corresponding to "$gt", "$gte", "$lt", "$lte", "$ne", " no special keywords "

>:d b.person.find ({"Age", {$gt: 20}})

<:d B.person.find ({"Age", {$lt: 20}})

=:d B.person.find ({"Age", 20})

And,or,in,notin " No keywords", "$or", "$in","$nin" respectively

And:db.person.find ({"Name": "Tom", "Age": 20})

Or:db.person.find ({$or: [{"Name": "Tom"},{"name": "Zhangsan"}]})

In:db.person.find ({"name": {$in: ["Zhangsan", "Tom"]}})

NotIn:db.person.find ({"Name", {$nin: ["Zhangsan", "Tom"]}})

Regular Expression db.person.find ({"Name":/^j/, "name":/e$/})

$where Db.person.find ({$where: function () {return this.name== "Tom"}})

Query number of records in the person collection db.person.count ()

Modify Operation

Overall modified db.person.update ({"Name", "Tom"},{"name": "Tom1", "Age":-})

Local modifications

$inc add increase abbreviation db.person.update ({"Name", "Tom"},{$inc: {"Age": +})// tom.age=40 after modification

$set Set db.person.update ({"Name": "Tom"},{$set: {"Age": +})// modified tom.age=20

Inserupdate Mode

Insert a new data if no data is found based on the first query condition

Db.person.update ({"Name": "Tom1"},{$set: {"age": 40}},true)

Batch Modification

MongoDB Default if you find more than one piece of data to modify based on a single query condition, only one is modified by default, and you can set the fourth parameter to true for bulk modification

Delete Operation Db.person.remove ({"Name": "Tom1"})

Aggregation operations

Count Total Records Db.person.count ()

Number of records in condition Db.person.count ({"Age": {$gt: +}})

Distinct db.person.distinct ("name") without conditions

Conditional de- db.person.distinct ("name", {"age": {$gt: +}})

Group Basic Group

Db.person.group ({"key": {"name": true}, "initial": {"person": []}, "Reduce": function (doc,out) {Out.person.push ( Doc.name+ ":" +doc.age)})

Show the number of each group

Db.person.group ({"key": {"Age": true}, "initial": {"person": []}, "Reduce": function (doc,out) {Out.person.push ( Doc.name+ ":" +doc.age)}, "Finalize": function (out) {out.count=out.person.length}})

Grouping with conditions

Db.person.group ({"key": {"Age": true}, "initial": {"person": []}, "Reduce": function (doc,out) {Out.person.push ( Doc.name+ ":" +doc.age)}, "Finalize": function (out) {out.count=out.person.length}, "condition": {"age": {$gt: 30}})

Mapreduce

First step: Create a mapping function

var map=function () {Emit (This.age,{count:1})}// grouping parameters

Step two: Create a simplified function

var reduce=function (key,value) {var result={count:0};    for (Var i=0;i<value.length;i++) {Result.count+=value[i].count; } return result; }

Step three: Execute the function

Db.person.mapReduce (map,reduce,{"Out": "Collection"})

Index operations

Build an index

Db.person.ensureIndex ({"Name": 1})//Add index to name field 1 for ascending order,1 the index that is established by default on behalf of descending order is not a unique index

Create a unique index

Db.person.ensureIndex ({"Name": 1},{"unique": true})

Set up a composite index

Db.person.ensureIndex ({"Name": 1, "Age":-1})

Gets the index index under the collection

Db.person.getIndexes ()

Delete Index

Db.person.dropIndex ({"Name": 1})

MongoDB Learning Notes

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.