Basic syntax for MongoDB

Source: Internet
Author: User
Tags auth

The previous period of work because there is not much to play with MongoDB, learn its basic grammar, and now do a simple summary here.

1. I am in win platform above, start words more trouble, so I simply put the boot process to do a batch file

Startup scripts

: Start a mongodb batch file , under    the bin directory of MongoDB D:    CD " program Files"    cd mongodb\server\3.2\bin: where D disk below the data, The DB directory was created manually : Start with the parameter --auth (Enable user access):--dbpath(datastore path)    mongod. exe-- Auth--dbpath D:\data\db

2. Some simple basic syntax

#创建或者切换数据库use admin# Query Method Db.system.users.find () #格式化的查询结果db. Userinfo.find (). Pretty () query comparer equals {<key>:<value>} db.col.find ({"By": "Rookie Tutorial"}). Pretty () Where by = ' Rookie Tutorial 'less than {<key>:{$lt: <value>}} db.col.find ({"likes": {$lt: {)}). Pretty () where likes < 50less than or equal to {<key>:{$lte: <value>}} db.col.find ({"likes": {$lte: {)}). Pretty () where likes <= 50greater than {<key>:{$gt: <value>}} db.col.find ({"likes": {$gt: {)}). Pretty () where likes > 50greater than or equal to {<key>:{$gte: <value>}} db.col.find ({"likes": {$gte: {)}). Pretty () where likes >= 50not equal to {<key>:{$ne: <value>}} db.col.find ({"likes": {$ne:}}). Pretty () where likes! = 50#查询年纪大于等于25的信息db. Userinfo.find ({"Age": {$gte: 25}}). Pretty () #查询age字段是String类型的记录.    The result set shown (the _id field is displayed by default) has only the user_name and age fields. Db.userinfo.find ({' Age ': {$type: 2}},{_id:0, "user_name": 1, "Age": 1}) #删除用户db. Dropuser (' Root ') #创建新的用户db. CreateUser ({' User ': ' Root ', ' pwd ': ' 000000 ', ' roles ': [{' Role ': ' ReadWrite ', ' db ': ' MyTest '}, {' Role ': ' ReadWrite ', ' db ': ' admin '}]}) #mongodb创建集合对象 db.xxx: Indicates that if XXX is present, insert data directly into it, and if it does not exist, create the corresponding collection # Insert data db. Collection_name.insert (document) Db.userinfo.insert ([{user_name:' 1 ', age:10, Sex:1},{user_name: ' 2 ', Age:10, sex:0}]) #mongodb更新语句更新单条语句 If the $set field does not exist and if the key does not exist, a new Db.item_info.update ({"item_id": "C9aea5ea-c29d-43ec-8205-fdb1e69934b7"},{$set: {"Item_name": "Test_update", "Test": "Test_add"}}) $unset keyword, delete the corresponding key value to remove the "Item_name", "test" the two key db.item_info.update ({"item_id": "C9aea5ea-c29d-43ec-8205-fdb1e69934b7"},{$unset: {"Item_name": "Test_update", "Test": "Test_add"}}) #mongodb更新语句更新多条语句db. Userinfo.updatemany ({"User_name": "1"}, {$set: {"user_name": "Nie"}}) #更新age字段<=The 20 field is Wewe value Db.userinfo.updateMany ({"Age": {$lte: $}}, {$set: {"Age": "Wewe"}}) #mongodb删除语句db. Userinfo.remove ({"User_name": "2"}) Delete all data Db.userinfo.remove ({}) #type $type operator is based on the Bson type to retrieve the matching data type in the collection and returns the result. //Querying the Age field is all records of type stringDb.userinfo.find ({"Age": {$type: 2}})
#MongoDB Sort () method using the sort () method to sort data in MongoDB, the sort () method can specify the sort field by parameter,and use 1 and-one to specify how to sort, where 1 is in ascending order, and-1 is for descending order. Db.userinfo.find ({},{_id:0, "user_name": 1, "Age": 1}). Sort ({"Age": 1}). Pretty () #创建索引 the option to add background:true when creating an index, Let the creation work execute in the backgroundDb.userinfo.ensureIndex ({"user_name": 1},{background:true})Maximum Range
    • Cannot have more than 64 indexes in a collection
    • Index name cannot exceed 125 characters in length
    • A composite index can have a maximum of 31 fields
 #MongoDB Aggregation Functions

#删除 Db.item_info.deleteOne ({"Item_name": "Update_item_name"}) Db.item_info.find ({"Item_name": "Update_item_name"}). Count () DeleteresultDelete= Item.deletemany (Filters.eq ("Item_name", "Update_item_name")); System.out.println (Delete. Getdeletedcount ()); #mongodb语句explain>db.item_info.find ({}). Explain () {"Queryplanner" : {                "Plannerversion": 1,                "Namespace": "Test.item_info",                "Indexfilterset":false,                "Parsedquery" : {                        "$and" : [ ]                },                "Winningplan" : {                        "Stage": "Collscan",                        "Filter" : {                                "$and" : [ ]                        },                        "Direction": "Forward"                },                "Rejectedplans" : [ ]        },        "ServerInfo" : {                "Host": "Lenovo-niejian",                "Port": 27017,                "Version": "3.2.8",                "Gitversion": "Ed70e33130c977bda0024c125b56d159573dbaf0"        },        "OK": 1} #mongodb语句like (regular expression) if the retrieval needs to be case-insensitive, we can set the $options to $i. The following command looks for a case-insensitive string W3cschool.ccdb.posts.find ({post_text:{$regex:"W3cschool.cc", $options: "$i"}}) Db.item_info.find ({"item_id": {$regex:/c9aea5ea/}})

Basic syntax for MongoDB

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.