MongoDB Database creation Delete, table creation Delete, data additions and deletions

Source: Internet
Author: User
Tags findone

I. Management of MONGODB database: MONGO

View All Database lists

Show DBS

Second, create a database

Create a database

Use student

If you really want to create this database successfully, insert the data (collections). There is no need to create a collection specifically, only db.student system Discovery is a strange collection name, so it displays the current set of data (called table), deletes the collection, deletes the specified collection 1, queries all records

Db.userInfo.find ();
Equivalent: select* from UserInfo;
2. Duplicate data for a column in the current clustered collection after the query is removed

Db.student.insert ({"Name": "x Iaom ing"});

Show collections

Db.dropdatabase ();

Delete the collection db. Collection_name.drop () Db.user.drop ()

db. Table name. Insert ({"name": "Zhangsan"}

);

Student Collection name (table)

Db.userInfo.distinct ("name"); Will filter out the same data in name

Equivalent: Select Distict name from UserInfo; 3. Check the record of age = 22

Db.userInfo.find ({"Age": 22});
Equivalent to: SELECT * from userInfo where age = 22;

4. Check the age > 22 record Db.userInfo.find ({age: {$gt: 22}});

Equivalent to: SELECT * from UserInfo where >22;

5. Check the records of age < 22

Db.userInfo.find ({age: {$lt: 22}});

Equivalent to: SELECT * from UserInfo where age <22;6, query age >= 25 Records

Db.userInfo.find ({age: {$gte: 25}}), equivalent to: SELECT * from UserInfo where age >= 25;

7, Query the age <= 25 record Db.userInfo.find ({age: {$lte: 25}});

8. Query age >= 23 and age <= 26db.userinfo.find ({age: {$gte: $, $lte: 26}});

9, query name contains MONGO data Db.userInfo.find ({name:/mongo/});

Note the writing format

Fuzzy queries for searching

Equal to percent
SELECT * from UserInfo where name is like '%mongo% ';

10. Query name Db.userInfo.find ({name:/^mongo/}) beginning with MONGO;
SELECT * from UserInfo where name is like ' mongo% ';

11. Query the specified column name, age data

Db.userInfo.find ({}, {name:1, age:1});

Equivalent to: Select Name, age from UserInfo;

Of course name can also be used with true or false, as in the case of Ture River Name:1 effect, if False is to exclude name, display column information other than name.

12. Query the specified column name, age data, age > 25db.userinfo.find ({age: {$gt: +}}, {name:1, age:1}), equivalent to: select Name, age from UserInfo Where age >25;13, sort by ages 1 ascending-1 Descending ascending: Db.userInfo.find (). Sort ({age:1}), descending: Db.userInfo.find (). Sort ({Age:-1});

14. Query name = Zhangsan, age = 22 data
Db.userInfo.find ({name: ' Zhangsan ', age:22});
Equivalent to: SELECT * from userInfo where name = ' Zhangsan ' and "age = ' 22 ';

15, query the first 5 data db.userInfo.find (). Limit (5); equivalent: selecttop 5 * from UserInfo;

16, query 10 after the data
Db. UserInfo. Find (). Skip (10);
Equivalent to: SELECT * from UserInfo where ID not in (selecttop * from UserInfo
);

17, query the data between 5-10
Db. Useri nfo. Fin d (). Limit (1 0). Skip (5);
Can be used for pagination, limit is Pagesize,skip is the first few pages *pagesize

18, OR and query
Db.userInfo.find ({$or: [{age:22}, {age:25}]}), equivalent to: SELECT * from userInfo where age = all or age = 25;19, findOne query First Data
Db. UserInfo. FindOne ();
Equivalent: Selecttop 1 * from Userinfo;db.userinfo.find (). limit (1);

20. Query the number of record bars in a result set

Db.userInfo.find ({age: {$gte: +}}). count (); equals: SELECT count (*) from UserInfo where age >= 20;

If you want to return the number of records after the limit, use count (true) or count (not 0) db.users.find (). Skip. Limit (5). Count (True);

Iv. Modification of data

There are also query conditions inside the changes. Who do you want to tell MONGO. Look for the name named Xiaoming, change the age to 16 years old:

Find Math score is 70, change age to 33 years old:

Change all matching items: "
By default, the update () method updates a single document. To update multiple documents, usethe multi option in the update () method.

Complete replacement, $set note

Db.users.update ({name: ' Lisi '}, {$inc: {age:50}}, False, True);

Equivalent to: Update users set age = Age + where name = ' Lisi ';d b.users.update ({name: ' Lisi '}, {$inc: {age:50}, $set: {name: ' Ho Ho '}}, False, True);

Equivalent to: Update users set age = Age +, name = ' HoHo ' WHERE name = ' Lisi ';

V. Deletion of data

1 db.student.update ({"Name": "Xiaoming"},{$set: {"Ag e": 16}});

1 db.student.update ({"SC Ore.shuxue": 70},{$set: {"Ag e": 33}});

1 db.student.update ({"Sex": "Male"},{$set: {"Age": 33}},{multi:true});

1 db.student.update ({"Name": "Xiaoming"},{"name": "Daming", "Age": 16});

Db.collectionsNames.remove ({"Borough": "Manhattan"})

Db.users.remove ({age:132});

By default, the Remove () method removes all documents that match the remove condition. usethe justone option to limit the remove operation to only one of the matching documents.

Db.restaurants.remove ({"Borough": "Queens"}, {justone:true})

MongoDB Database creation Delete, table creation Delete, data additions and deletions

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.