Mongodb addition, deletion, modification, and query
* ******************************** 1. insert ******************************** use tblorders; -- Method 1db. tblorders. insert ({orderno: "A2014089901", pname: "tblorders", scity: "beijing"}); db. tblorders. insert ({orderno: "A2014089902", pname: "snow", scity: "Chengdu"}); db. tblorders. insert ({orderno: "A2014089903", pname: "kiki", scity: "Chongqing"}); db. tblorders. find (); -- Method 2db. tblorders. save ({orderno: "A2014089904", pname: "atalas", scity: "Urumqi", sdate: "2015-08-08"}); -- Method 3for (var I = 1; I <= 300; I ++) db. tblorders. save ({id: I, name: 'ocpyang '}); * ******************************** 2.: set updates all ocpyang to Atalasdb. tblorders. update ({name: "ocpyang" },{$ set: {name: "Atalas" }}, false, true); --- method: $ inc usage: {$ inc: {field: value} indicates adding value to a numeric field, for example, db. tblorders. insert ({orderno: "10001", pname: "ocpyang", scity: "Chongqing", price: 1500}); db. tblorders. find ({"pname": "ocpyang "}). forEach (printjson); {"_ id": ObjectId ("55bf126b4726e2d2dc5f43cd"), "orderno": "10001", "pname": "ocpyang", "scity ": "Chongqing", "price": 1500} db. tblorders. update ({"orderno": "10001" },{$ inc: {"price": 130 }}); db. tblorders. find ({"pname": "ocpyang "}). forEach (printjson); {"_ id": ObjectId ("55bf126b4726e2d2dc5f43cd"), "orderno": "10001", "pname": "ocpyang", "scity ": "Chongqing", "price ": 1630} ********************************* 3. delete ********************************* $ unset usage: {$ unset: {field: 1 }}-- 1. A row of db that meets the condition. tblorders. update ({"id": 1 },{$ unset: {"naje": 1 }}); -- 2. all rows that meet the condition. tblorders. update ({"name": "Atalas" },{$ unset: {"name": 1 }}, false, true ); * ******************************** 4. query ********************************** db. tblorders. insert ({orderno: "10001", pname: "ocpyang", scity: "Chongqing", price: 1500}); db. tblorders. insert ({orderno: "10005", pname: "luces", scity: "Tianjin", price: 1280}); db. 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 dB. tblorders. find ({"pname": "kiki "}). limit (1); -- Method 4db. tblorders. find ({"pname": "kiki "}). forEach (printjson); db. tblorders. find ({"pname": "Andy Lau "}). forEach (printjson); -- Method 5: greater than, greater than or equal to, less than, less than or equal to, betweendb. tblorders. find ({"price": {$ gt: 1500 }}). forEach (printjson); db. tblorders. find ({"price" :{$ gte: 1500 }}). forEach (printjson); db. tblorders. find ({"price" :{$ lt: 1500 }}). forEach (printjson); db. tblorders. find ({"price": {$ lte: 1500 }}). forEach (printjson); -- greater than 1700 is less than analyticdb. 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 not equal to 1630db. tblorders. find ({"price": {$ ne: 1630, $ gt: 1300, $ lt: 10000 }}). forEach (printjson); -- Method 7: indb. tblorders. find ({"price": {$ gt: 1000 }}). 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: []}). 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: [1000], $ 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 returns the starting database of the record. tblorders. find (). skip (2 ). limit (5); # returns 5 Records starting from 3rd -- Method 10: sort db. tblorders. find ({"price": {$ gt: 0 }}). sort ({price: 1 }). forEach (printjson); db. tblorders. find ({"price": {$ gt: 0 }}). sort ({price: 1}); db. 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.. tblorders. find (). count ();
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.