Use of MongoDB

Source: Internet
Author: User

Recently the project has used MongoDB, recently like MongoDB NoSQL database is also relatively hot, write this down, easy to review and learn from you.

The relationship between the data in MongoDB is database,collection,document put into the relational database is the library, table, data.


1. Perform the sudo apt-get install MONGO installation MONGO under Ubuntu.


2. Enter MONGO to enter the database.

View collection (table) show collections;


View data for a student collection

Db.student.find ();

Db.student.find (). ForEach (Printjson);

View third to sixth records

Db.student.find (). Skip (2). Limit (3);

Db.student.find (). ForEach (Printjson); ---Viewing records in JSON format

Inserting data into the student collection

Db.student.insert ({name: "Jack", age:22,score:86});


Delete a piece of data

Db.student.remove ({name: "Jack"});


modifying data

Db.student.update ({name: "Tom"},{$set: {name: "Lubby"}});


Conditional query:

Db.student.find ({score:{$gt: 86}}); Score>86

Db.student.find ({score:{$gte: 86}}); Score>=86

Db.student.find ({score:{$lt: 100}}); score<100

Db.student.find ({score:{$lte: 100}}); score<=100

Db.student.find ({score:{$gt: $lt: 100}}); 87<score<100

Db.student.find ({name:{$ne: "Lubby"}}); Name! = Lubby


Show data that exists for a field

Db.student.find ({name:{$exists: true});

Query mismatch name=b* lead record
Db.users.find ({name: {$not:/^b.*/}});


Regular expression Matching


Sort

Db.student.find (). Sort ({score:1}); Sort by score Ascending

Db.student.find (). Sort ({score:-1}); Sort in descending order of score


There are some similar groups of queries back time to fill up.



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.