MongoDB database operations, additions and deletions to check

Source: Internet
Author: User

Inserting some data into the student collection

Db.student.insert ({"Study No.": 10010, "name": "De Levin", "Age": 26, "Address": "Zibo"}), var studata1={"study number": 10020, "name": "Obama", "age": 27, "Address": "Azeroth"};d B.student.insert (stuData1), var studata2={"School Number": 10030, "name": "Female Gun", "age": 27, "Address": "Azeroth"};d B.student.insert ( STUDATA2) var studata3={"Study No.": 10050, "name": "Rui Wen", "age": 18, "Address": "Super Theological Seminary"};d B.student.insert (STUDATA3);

Insert Array (data is JSON)

var studata=[{"name": "Anner", "Age": 26},{"name": "James", "Age": 28},{"name": "Allen", "Age": 31},{"name": "Smith", "Age": 17}]; Db.student.insert (Studata);

Insert (), use JavaScript to manipulate

Add 10,000 data for (Var i=0;i<10000;i++) {Db.student.insert ({"Vid": "www.oracle.com" +i+ "line"});};

View data

Db.student.find ();

Data query

Example 1: Query for records with number 10020

Db.student.find ({"Study number": 10020});d B.student.find ({"Study number": 10020}). Pretty ();

Example 2: A query that does not display _id

Db.student.find ({"Study number": 10020},{"_id": 0});

Relational operations

> $GT
< $LT
>= $gte
<= $lte
! = $ne
= Key:value $eq

Define a collection of employee information

Db.employee.drop ();d B.employee.insert ([{"Name": "Smith", "age": +, "sex": "Male", "job": "Professor", "Addr": "Peking University", "cellphone" : "17166660001"},{"name": "Clark", "Age": "Sex": "Male", "job": "Lecturer", "addr": "Yanjing University", "cellphone": "17166660002"},{"name" : "Allen", "Age": "Sex": "female", "job": "Director", "addr": "Nanjing University", "cellphone": "17166660003"},{"name": "Anner", "Age": "Sex" : "Female", "job": "Propaganda", "addr": "University of Tokyo", "cellphone": "17166660004"},{"name": "James", "age": +, "sex": "Male", "job": "Academic", "addr": "University of Western Beijing", "cellphone": "17166660005"}]);d b.employee.find ();d b.employee.find (). pretty ();

Example 3: Querying for records with name Anner

Db.employee.find ({"Name": "Anner"}). Pretty ();

Example 4: Querying for a gender-male record

Db.employee.find ({"Sex": "Male"}). Pretty ();

Example 5: Querying for records older than 19 years

Db.employee.find ({"Age": {"$GT":}}). Pretty ();

Example 6: Querying for records with age less than or equal to 19 years

Db.employee.find ({"Age": {"$lte":}}). Pretty ();

Example 7: Query gender is not a woman's record

Db.employee.find ({"Sex": {"$ne": "Female"}}). Pretty ();

Logical operations

and $and
or $or
Non-$not $nor

Example 8: Querying a record of age between 10~20

Db.employee.find ({"Age": {"$GT": Ten, "$lte": $}});d B.employee.find ({"Age": {"$GT": Ten, "$lte":]}). Pretty ();

Example 9: Query age is not 19 years old record

Db.employee.find ({"Age": {"$ne": 19}});

Example 10: Querying for records older than 19 years, or sex for women

Db.employee.find ({"Age": {"$GT": +}},);d B.employee.find ({"$or": [{"$GT": "19}},{": {"$eq": "Female"}]});

Example 11: Reverse the previous question

Db.employee.find ({"$nor": [{"Age": {"$gt": 19}},{"Sex": {"$eq": "Female"}]});

Modulo operation "$mod"

Example 12: Finding the record of age Mod27 1, which is 28

Db.employee.find ({"Age": {"$mod": [27,1]}});

  

Scope Query
$in $nin

Example 13: Query name is a record of allen,anner,james

Db.employee.find ({"name": {"$in": ["Allen", "Anner", "James"]}});

Example 14: Querying for records whose names are not allen,anner,james

Db.employee.find ({"name": {"$nin": ["Allen", "Anner", "James"]}})

Building a collection Employee collection

Db.employee.insert ([{"Name": "Jones", "age": +, "sex": "Male", "job": "Technician", "addr": "Harvard University", "cellphone": "15166660001"},{" Name ":" Blake "," age ": +," sex ":" Male "," job ":" Salesperson "," addr ":" Oxford University "," cellphone ":" 15166660002 "},{" name ":" Martin "," Age " : +, "sex": "Male", "job": "Salesperson", "addr": "Cambridge University", "cellphone": "15166660003"},{"name": "Turner", "age": +, "sex": "female", "Job": " Logistics "," addr ":" Peking University "," cellphone ":" 15166660004 "},{" name ":" Miller "," age ": All," sex ":" female "," job ":" Administration "," addr ":" Nanjing University "," Cellphone ":" 15166660005 "}]);

Example 15: Query job is not a "technician" record

Db.emp.find ({"Jod": {"$nin": ["Technician"]}});

  

MongoDB database operations, additions and deletions to check

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.