MongoDB additions and deletions to search

Source: Internet
Author: User

1. Insert the ********************************use tblorders;--method 1db.tblorders.insert ({ OrderNo: "A2014089901", PName: "Tblorders", Scity: "Beijing"});d B.tblorders.insert ({orderno: "A2014089902", PName: "SN Ow ", scity:" Chengdu "});d B.tblorders.insert ({orderno:" A2014089903 ", PName:" Kiki ", Scity:" Chongqing "});d B.tblorders.find ();--method 2db.tblorders.save ({orderno: "A2014089904", PName: "Atalas", Scity: "Urumqi", Sdate: "2015-08-08"});--Method 3for (var i = 1; I &l t;= 300; i++) Db.tblorders.save ({id:i, Name: ' Ocpyang '}); ********************************2. Update ***************************** ---method: Set update all Ocpyang to Atalasdb.tblorders.update ({name: "Ocpyang"}, {$set: {name: "Atalas"}},false,true);---method: $  Inc$inc Use method: {$inc: {Field:value}} means add value to a numeric Field field, Example: Db.tblorders.insert ({orderno: "10001", PName: "Ocpyang", Scity: "Chongqing", price:1500});d B.tblorders.find ({"PName": "Ocpyang"}). ForEach (Printjson); {"_id": ObjectId ("55BF126B4726E2D2DC5F43CD"), "OrderNo": "10001", "PName": "Ocpyang "," scity ":" Chongqing "," Price ": 1500}db.tblorders.update ({" OrderNo ":" 10001 "}, {$inc: {' price ': ']}});d B.tbl Orders.find ({"PName": "Ocpyang"}). ForEach (Printjson); {"_id": ObjectId ("55BF126B4726E2D2DC5F43CD"), "OrderNo": "10001", "pname": "Ocpyang", "scity": "Chongqing", "Price": 1630}**** 3. Delete ******************************** $unset use: {$unset: {field:1}}--1.  A row db.tblorders.update ({"id": 1}, {$unset: {"Naje": 1}}) that satisfies the condition,--2. All rows that satisfy the condition Db.tblorders.update ({"Name": "Atalas"}, { $unset: {"name": 1}},false,true); ********************************4. Query ******************************** Db.tblorders.insert ({orderno: "10001", PName: "Ocpyang", Scity: "Chongqing", price:1500});d B.tblorders.insert ({orderno: "100 ", PName:" Luces ", scity:" Tianjin ", price:1280});d B.tblorders.insert ({orderno:" 10010 ", PName:" Andy Lau ", Scity:" Shanghai Pudong ", Ecity: "Houston", price:9850});--Method 1db.tblorders.find ();--Method 2db.tblorders.find ({"PName": "Kiki"});--Method 3db.tblorders.find ({" PName ":" Kiki "}). LiMIT (1);--Method 4db.tblorders.find ({"PName": "Kiki"}). foreach (Printjson);d b.tblorders.find ({"PName": "Andy Lau"}). foreach ( Printjson);--Method 5: Greater than, equal to, less than, less than or equal to, Betweendb.tblorders.find ({"Price": {$gt: ()}). ForEach (Printjson); Db.tblorders.find ({"Price": {$gte:). foreach (Printjson);d the B.tblorders.find ({"Price": {$lt:}}). foreach ( Printjson);d b.tblorders.find ({"Price": {$lte: ()). ForEach (Printjson);--greater than 1700 less than 10000db.tblorders.find ({" Price ": {$gt: 1700, $lt: 10000}}). foreach (Printjson);--Method 6: Not equal to Db.tblorders.find ({" Price ": {$ne: 1630}}). foreach ( Printjson); eg: greater than 1300 less than 10000 is not equal to 1630db.tblorders.find ({"Price": {$ne: 1630, $GT: 1300, $LT: 10000}}). ForEach (Printjson );--Method 7:indb.tblorders.find ({"Price": {$gt: +}}). ForEach (Printjson); {"_id": ObjectId ("55BF126B4726E2D2DC5F43CD"), "OrderNo": "10001", "pname": "Ocpyang", "scity": "Chongqing", "Price": 1630} {"_id": ObjectId ("55bf15bd4726e2d2dc5f43ce"), "OrderNo": "10001", "pname": "Ocpyang", "scity": "Chongqing", "Price": 1500} {"_id": ObjectId ("55bf15bd4726e2d2dc5f43CF ")," OrderNo ":" 10005 "," pname ":" Luces "," scity ":" Tianjin "," Price ": 1280}{" _id ": ObjectId (" 55bf15be4726e2d2dc5f43d0 ")," OrderNo ":" 10010 "," PName ":" Andy Lau "," scity ":" Shanghai Pudong "," ecity ":" Houston "," Price ": 9850}db.tblorders.find ({" Price ": {$in: [12 80,1500]}). ForEach (Printjson); {"_id": ObjectId ("55bf15bd4726e2d2dc5f43ce"), "OrderNo": "10001", "pname": "Ocpyang", "scity": "Chongqing", "Price": 1500} {"_id": ObjectId ("55BF15BD4726E2D2DC5F43CF"), "OrderNo": "10005", "pname": "Luces", "scity": "Tianjin", "Price": 1280}--Method 8: Not Indb.tblorders.find ({' price ': {$nin: [1280,1500], $GT: +}}). ForEach (Printjson); {"_id": ObjectId ("55BF126B4726E2D2DC5F43CD"), "OrderNo": "10001", "pname": "Ocpyang", "scity": "Chongqing", "Price": 1630} {"_id": ObjectId ("55bf15be4726e2d2dc5f43d0"), "OrderNo": "10010", "PName": "Andy Lau", "scity": "Shanghai Pudong", "ecity": "Houston", "  Price ": 9850}--method 9:skip limit the return record's starting point Db.tblorders.find (). Skip (2). Limit (5); #从第3条開始返回5条-Method 10:sort sort Db.tblorders.find ({"Price": {$gt: 0}}). sort ({price:1}). ForEach (Printjson);d b.tblorders.find ({"Price": {$gt: 0}}). sort ({price:1});d B.tblorders.find ({' price ': {$gt: 0}}). Sort ({price:-1}); --Method 11: Cursor for (var c = db.tblorders.find ({"Price": {$gt: 0}}), C.hasnext ();) {Printjson (C.next ());} Db.tblorders.find ({"Price": {$gt: 0}}). ForEach (function (u) {printjson (U);}); 5. Statistics ********************************db.tblorders.find (). Count ();


MongoDB additions and deletions to search

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.