MongoDB Learning Notes

Source: Internet
Author: User
Tags mongodb query mongodb tutorial mongodb update

1, each boot needs to configure the boot file Mongod--dbpath G:\data\db execute the above command and then open a new window to perform mongo 2, show dbs #查看所有数据库 use MyDB #使用mydb数据库 show C Ollections #查看该数据库下所有的表  3, MongoDB query Db.col.find () #查看col集合下所有的document (document equivalent to row in MySQL) Db.col.find () . Pretty () #以格式化的方式来显示所有文档 Db.col.find ({"By": "Rookie Tutorial"}) #条件查询相当于sql中 where by = ' Rookie Tutorial ' Db.col.find ({"likes": {$lt: ()}) # WH Ere likes < Db.col.find ({"likes": {$lte: +}) #where likes <= Db.col.find ({"likes": {$gt: +}) # where Likes & Gt Db.col.find ({"likes": {$gte:) #where likes >= Db.col.find ({"likes": {$ne:]}) #where likes! = Db.col.fin D ({"By": "Rookie Tutorial", "title": "MongoDB Tutorial"}) #WHERE by= ' rookie tutorial ' and title= ' MongoDB Tutorial ' Db.col.find ({$or: [{"By": "Rookie Tutorial"},{"title ":" MongoDB Tutorial "}]}) #WHERE by= ' rookie tutorial ' OR title= ' MongoDB Tutorial ' db.col.find ({" likes ": {$gt: $or: [{" By ":" Rookie Tutorial "},{" title ": "MongoDB Tutorial"}]}) #where likes>50 and (by = ' Rookie Tutorial ' or title = ' MongoDB tutorial ')  4, MongoDB delete db.col.remove ({' title ': ' MongoDB Tutorial '}) #移除 title ' MOngodb Tutorial ' document Db.col.remove ({}) #删除col中的数据  5, insert Db.col.insert in two MongoDB ({title: ' MongoDB Tutorial ', Description: ' Mong  ODB is a Nosql database ' by: ' Rookie tutorial ', url: ' http://www.runoob.com ', Tags: [' mongodb ', ' database ', ' NoSQL '], likes:100})   Document= ({title: ' MongoDB Tutorial ', Description: ' MongoDB is a Nosql database ', by: ' Rookie tutorial ', url: ' http://www.runoob.com ', Tags: [' m  Ongodb ', ' database ', ' NoSQL '], likes:100}); Db.col.insert (document)  6, MongoDB update db.col.update ({' title ': ' MongoDB Tutorial '},{$set: {' title ': ' MongoDB '}}) #通过 The update () method updates the title (title)   #save () method to replace an existing document with an incoming document Db.col.save ({     "_id": ObjectId (" 56064f89ade2f21f36b03136 ")," title ":" MongoDB "," description ":" MongoDB is a Nosql database "," by ":" Runoob "," url ":" http:/ /www.runoob.com "," tags ": [" MongoDB "," NoSQL "]," likes ": ()) #替换了 _id for 56064f89ade2f21f36b03136 document data  7, Mongo Sorting in DB uses the sort () method to sort the data, and the sort () method can specify the sorted fields by parameter, where 1 is in ascending order, and 1 is for descending order Db.col.find ({},{"title": 1,_id:0}). Sort ({" Likes ": -1}) #col the data in the collection is sorted in descending order by field likes  8, index Db.col.ensureIndex in MongoDB ({"title": 1}) #为title字段建立索引, 1 is ascending, 2 is descending  Db.col.ensureIndex ({"title": 1, "description": -1}) #使用多个字段创建索引 (called composite Index in relational database)  9, aggregations in MongoDB (aggregate) Mainly used to process data (such as statistical averages, sums, etc.) and return the computed data results. A bit like the count (*) db.mycol.aggregate in the SQL statement ([{$group: {_id: "$by _user", num_tutorial: {$sum: 1}}])   #集合计算每个作者所写的文章数 , $sum calculates the sum, similar to select By_user as _id, COUNT (*) as num_tutorial from MyCol Group by by_user 

MongoDB Learning Notes

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.