MongoDB Installation and CRUD operations

Source: Internet
Author: User

What is Mongodb?       

Mongo DB is an open-source, non-relational database (NOSQL) whose document model is free and flexible, allowing you to be more comfortable in the development process. MongoDB can handle Internet applications with large data volumes, high concurrency, and weak transactions. MongoDB's built-in horizontal scaling mechanism provides the ability to handle data volumes from millions to 1 billion levels, fully satisfying the data storage needs of Web2.0 and mobile Internet, and its out-of-the-box features greatly reduce the operational costs of small and medium-sized websites.


Installing MongoDB

Come to MongoDB official website http://www.mongodb.org/can find the Yum source here, install the corresponding version of the two packages, one for the server, one for the client installation package name is as follows:

mongo-10gen-2.4.12-mongodb_1.x86_64.rpm

mongo-10gen-server-2.4.12-mongodb_1.x86_64.rpm

Add the official Yum Source:

vim /etc/yum.repos.d/monogdb.repo

[mongodb] name=MongoDB Repository baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64/ gpgcheck=0 enabled=1

Yum Install mongo-10gen.x86_64 mongo-10gen-server.x86_64-y

Create a MongoDB Data folder

mkdir/mongodb/data/

Chown-r mongod.mongod/mongodb/data/

Modify configuration file/etc/mongod.conf Specify Data directory

Dbpath=/mongodb/data

Start MongoDB

Service Mongod Start

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/57/9A/wKiom1SfyE6TZq64AAIM0J8VHCY345.jpg "title=" 4ush%} @NX ~x]bsvpnq@u748.png "alt=" Wkiom1sfye6tzq64aaim0j8vhcy345.jpg "/>

The CRUD operations of MongoDB


[Email protected] ~]# MONGO

MongoDB Shell version:2.4.5

Connecting To:test

> Show DBS //Display database

Local0.078125GB

TestDB0.203125GB

> Use TestDB //using a database that does not need to be created in advance can be used directly.

Switched to DB TestDB

> Db.testmcoll.insert ({Name: "Jerry"}) //Specify collection to insert data

> Db.testmcoll.insert ({Name: "Haiman"})

> Show Collections //Show collections in library

System.indexes

Testmcoll

> Db.testmcoll.find () //Find data in a collection

{"_id": ObjectId ("549fcaa56e8223a06e8b1f52"), "Name": "Jerry"}

{"_id": ObjectId ("549fcadc6e8223a06e8b1f53"), "Name": "Haiman"}

> db.testmcoll.stats () //Output set status information

{

"NS": "Testdb.testmcoll",

"Count": 2,

"Size": 80,

"Avgobjsize": 40,

"Storagesize": 4096,

"Numextents": 1,

"Nindexes": 1,

"Lastextentsize": 4096,

"Paddingfactor": 1,

"Systemflags": 1,

"UserFlags": 0,

"Totalindexsize": 8176,

"Indexsizes": {

"_id_": 8176

},

"OK": 1

}

>

> Db.testmcoll.drop () //Delete collection

True

Multi-value inserts and bulk inserts

Db.users.insert ({name: "Tom", age:23, Status: "S", groups:["News", "Concert"]})

for (i=1;i<=100;i++) Db.users.insert ({Name: "User" +i,age:i,gender: "M", class:["Network", "Software"]})

Delete a line of age 18

Db.users.remove ({age:18})

Change the age of User12 to 22

Db.users.update ({Name: "User10"},{$set: {age:22}}) //Specify the query criteria first, then separate the values you want to set with commas.

Change the value of the User class field older than 60 to "Old-club"

Db.users.update ({age:{$gt: 60}},{$set: {Class: "Old-club"}},{multi:true}) //mongodb By default only the document for the first match condition is modified, Multi:true represents modifying the content of the entire document to be matched to

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/57/98/wKioL1Sf8InxXtC5AAHUAWO01x4254.jpg "title=" Qq20141228195546.png "alt=" Wkiol1sf8inxxtc5aahuawo01x4254.jpg "/>

Count the number of documents in the specified collection

Db.users.count ()

Db.collection.find () comparison operators:

$GT Greater than

$gte greater than or equal to

$in exists in the specified list

$lt less than

$lte less than or equal to

$ne Not equal to

$nin does not exist in the specified list


Find a user with age greater than or equal to 90 and display only the Name,age field:

Db.users.find ({age:{$gte: 90}},{name:1,age:1})


Logical operations

$or: Or Operation $and: $not with Operation: non-operational $nor: Inverse operation, which returns documents that do not meet all specified criteria.

Find users with age greater than 61 and less than 70

Db.users.find ({$and: [{age:{$gt: 61}},{age:{$lt: 70}]})

Look for users older than 61 and less than 70 only, display the name field

Db.users.find ({$and: [{age:{$gt: 61}},{age:{$lt: 70}}]},{name:1})

Element query

$exists: Selects the document according to the existence of the specified field, syntax format {field:{$exists: <boolean>}, specifying the value of <boolean> to "true" returns the document with the specified field, "false" The document that does not exist for the specified field is returned;


$mod: Takes the value of the specified field to the modulo operation and returns the remaining number as the specified worthy document


$type: Returns a document of the specified field with a value type of the specified type


Find documents that have an address field

Db.users.find ({address:{$exists: true}})


Summary complete!

This article is from the "Break Comfort zone" blog, so be sure to keep this source http://tchuairen.blog.51cto.com/3848118/1597002

MongoDB Installation and CRUD operations

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.