Learn MONGO series (d) Find (). Pretty () Remove ()

Source: Internet
Author: User

The Find () method displays all documents in an unstructured manner.

If you need to read the data in an easy-to-read way, you can use the pretty () method with the following syntax: Db.collection_name.find (). Pretty ()

The update () function can be used for MONGODB data updates. It is a good practice to execute the Find () command before executing the Remove () function to determine if the condition of execution is correct.

> Db.user.save ({name: "xxxxx", Password: "11111"})
Writeresult ({"ninserted": 1})
> Db.user.find (). Pretty ()
{
"_id": ObjectId ("56946FBA3A18F4867AECBCD1"),
"Name": "Admin",
"Password": "123456",
"title": "Admin"
}
{
"_id": ObjectId ("5694714A3A18F4867AECBCD2"),
"Name": "Aaaaaaaaa",
"Password": "123456"
}
{
"_id": ObjectId ("569476A63A18F4867AECBCD3"),
"Name": "XXXXX",
"Password": "11111"
}
> Db.user.remove ("name": "xxxxx")
2016-01-12t11:45:41.976+0800 E QUERY [Thread1] syntaxerror:missing) after a
Rgument list @ (Shell): 1:21

> Db.user.remove ({"Name": "XXXXX"})
Writeresult ({"nremoved": 1})
> is still not paying attention to checking parentheses

> Db.user.find (). Pretty ()
{
"_id": ObjectId ("56946FBA3A18F4867AECBCD1"),
"Name": "Admin",
"Password": "123456",
"title": "Admin"
}
{
"_id": ObjectId ("5694714A3A18F4867AECBCD2"),
"Name": "Aaaaaaaaa",
"Password": "123456"
}
>

If you want to delete all the data, you can use the following methods (like the General SQL truncate command):

Db.user.remove ()

MongoDB vs. RDBMS Where statement Comparison (table from http://www.runoob.com/mongodb/mongodb-query.html)

If you are familiar with regular SQL data, the following table provides a better understanding of MongoDB conditional statement queries:

Operation format Example similar statements in an RDBMS
Equals {<key>:<value>} db.col.find({"by":"菜鸟教程"}).pretty() where by = ‘菜鸟教程‘
Less than {<key>:{$lt:<value>}} db.col.find({"likes":{$lt:50}}).pretty() where likes < 50
Less than or equal to {<key>:{$lte:<value>}} db.col.find({"likes":{$lte:50}}).pretty() where likes <= 50
Greater than {<key>:{$gt:<value>}} db.col.find({"likes":{$gt:50}}).pretty() where likes > 50
Greater than or equal to {<key>:{$gte:<value>}} db.col.find({"likes":{$gte:50}}).pretty() where likes >= 50
Not equal to {<key>:{$ne:<value>}} db.col.find({"likes":{$ne:50}}).pretty() where likes != 50

> db.user.update ({"Name": "Admin"},{$set: {"Grade": 50}})
Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1})
> db.user.update ({"Name": "Aaaaaaaaa"},{$set: {"Grade": 50}})
Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1})
> Db.user.find (). Pretty ()
{
"_id": ObjectId ("56946FBA3A18F4867AECBCD1"),
"Name": "Admin",
"Password": "123456",
"title": "Admin",
"Grade": 50
}
{
"_id": ObjectId ("5694714A3A18F4867AECBCD2"),
"Name": "Aaaaaaaaa",
"Password": "123456",
"Grade": 50
}
> db.user.update ({"Name": "Aaaaaaaaa"},{$set: {"Grade": 99}})
Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1})
> Db.user.find (). Pretty ()
{
"_id": ObjectId ("56946FBA3A18F4867AECBCD1"),
"Name": "Admin",
"Password": "123456",
"title": "Admin",
"Grade": 50
}
{
"_id": ObjectId ("5694714A3A18F4867AECBCD2"),
"Name": "Aaaaaaaaa",
"Password": "123456",
"Grade": 99
}is still very careless, cause behind the {"name": "AAAAAAAAA"} first update the score to 50, and then update the score to 99, it was the first time to update to 99.

> Db.user.find ({"Grade": {$gt:]}). Pretty ()
{
"_id": ObjectId ("5694714A3A18F4867AECBCD2"),
"name" : "Aaaaaaaaa",
"password": "123456",
"grade":
}
> Db.user.find ({"Grade": {$gte:)}). Pretty ()
{
"_id": ObjectId ("56946FBA3A18F4867AECBCD1"),
"name": "admin",
"password": "123456",
"title": "Admin" ,
"Grade":
}
{
"_id": ObjectId ("5694714A3A18F4867AECBCD2"),
"name": "Aaaaaaaaa",
"password": "123456",
"Grade":
}

> Db.user.find ({"Grade": {$ne: ()}). Pretty ()
{
"_id": ObjectId ("56946FBA3A18F4867AECBCD1"),
"Name": "Admin",
"Password": "123456",
"title": "Admin",
"Grade": 50
}
> Db.user.find ({"Grade": {$lte: +}). Pretty ()
{
"_id": ObjectId ("56946FBA3A18F4867AECBCD1"),
"Name": "Admin",
"Password": "123456",
"title": "Admin",
"Grade": 50
}
{
"_id": ObjectId ("5694714A3A18F4867AECBCD2"),
"Name": "Aaaaaaaaa",
"Password": "123456",
"Grade": 99
}

Mess so much, useful on a few words, forgive me!

Learn MONGO series (d) Find (). Pretty () Remove ()

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.