mongdb--3. Query operations

Source: Internet
Author: User

1. Preparatory work

(1) Start MONGO

Enter the bin/directory of the MONGO installation directory,./mongod

(2) Start MONGO client

./mongo

(3) View all libraries

Show DBS;

(4) Switch to LVYF Library

Use LVYF

(5) View all collections

Show collections;

(6) Check data from a collection

Db.first_col.find ();

2. Enquiry

(1) View all, pretty () function is to format the JSON data

Db.collection_name.find (). pretty ();

>"_id": ObjectId ("575e879380d09b176a58ce64"), "name": "Lvyafei", "age": +, "sex": "M", "Address": "http: www.lvyf.com "_id": ObjectId ("575e880980d09b176a58ce65"), "name": "Chenjl", "Age": $, "Sex": "W", "address ":" http://www.chenjl.com "" _id ": ObjectId (" 575e889880d09b176a58ce66 ")," name ":" Child "," Age ": 2," Sex ":" M " , "Address": "Http://www.child.com", "status": "Cry and Laugh"}

(2) Conditional query

Operation

Format Example The corresponding SQL statement       

Equals

{Key:value} {NAME:LVYF} where Name= ' Lvyf '

Less than

{key: {&lt:value}} {age: {&lt:20}} Where Age < 20

Less than or equal to

{key: {&lte:value}} {age: {&lte:20}} where Age <=20

Greater than

{key: {&gt:value}} {age: {&gt:3}} Where Age > 3

Greater than or equal to

{key: {&gte:vlaue}} {age: {&gte:3}} where Age >=3
Not equal to {key: {&ne:value}} {name: {&NE:LVYF}}

WHERE name! = ' LVYF '

(3) and conditions

Query records based on gender and name, corresponding SQL statement where name= ' Lvyafei ' and sex = ' M '

Db.first_col.find ({name: "Lvyafei", Sex: "M"}). Pretty (){    "_id": ObjectId (" 575e879380d09b176a58ce64 "),    " name ":" Lvyafei ",    " age ": +,    " sex ":" M " ,    " Address ":" Http://www.lvyf.com "}

(4) or condition, grammar

Db.collection_name.find ({$or: [{key1:value1}, {key2:value2}]  });

Search examples by name or gender:

>db.first_col.find ({$or: [{name: ' Lvyafei '},{sex: ' M '}]})' _id ': ObjectId ("  575e879380d09b176a58ce64 ")," name ":" Lvyafei "," age ": +," sex ":" M "," Address ":" http://www.lvyf.com "" _id ": ObjectId ("575e889880d09b176a58ce66"), "name": "Child", "Age": 2, "Sex": "M", "Address": "Http://www.child.com", "stat US ":" Cry and Laugh "}

(5) combined with and and or, equivalent to where age >= 3 and (name= ' Lvyafei ' or sex= ' M ')

Db.first_col.find ({age: {$gte: 3}, $or: [{name: ' Lvyafei '}, {set: ' M '}]}). Pretty (){    "_ ID ": ObjectId (" 575e879380d09b176a58ce64 "),    " name ":" Lvyafei ",    " age ":    "Sex": "M",    "address": "Http://www.lvyf.com"}

(6) greater than and less than union query, equivalent to where age > 3 and Age < 21

Db.first_col.find ({"Age": {$lt:, $GT: 3}})"_id": ObjectId ("575e880980d09b176a58ce65"), "name": " Chenjl "," age ": +," sex ":" W "," Address ":" Http://www.chenjl.com "}

(7) $type operator

If you want to query that name is a string type of record

> Db.first_col.find ({name: {$type: 2}});" _id ": ObjectId (" 575e879380d09b176a58ce64 ")," name ":" Lvyafei "," age ": +," sex ":" M "," Address ":" Http://www.lvyf.com "_id": ObjectId ("575e880980d09b176a58ce65"), "name": "Chenjl", "age": +, "sex": "W", "Address": "Http://ww w.chenjl.com "_id": ObjectId ("575e889880d09b176a58ce66"), "name": "Child", "Age": 2, "Sex": "M", "Address": "Http://www.child.com", "status": "Cry and Laugh"}

mongdb--3. Query operations

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.