The basic crud for MongoDB learning notes

Source: Internet
Author: User

Mongo command:

    • Show Databases/dbs;
    • Use test;
    • Show Tables/collections;
    • Db.help ()
    • Db.createcollection (' user ')
    • Db.user.insert ({name: ' Sky ', age:22})
    • Db.user.insert ({_id:2, Name: ' Allen ', age:23})
    • Db.user.find ()
    • Db.user.drop ();
    • Db.dropdatabase ();
    • Db.stu.remove ({sn: ' 001 '})
    • Db.stu.remove ({sn: ' 001 '}, True) #第二个属性表示只影响一行
    • Db.stu.update ({name: ' Liubei '},{name: ' Guanyu ', age:20}) # The update in MONGO is a new document that replaces the old document directly, not the modification. If you change only one field, you need to use the set
    • Db.stu.update ({name: ' Guanyu '},{$set: {name: ' Sky '}})

Update Specific actions:

    • $set Modify the value of a column
    • $unset Delete a column
    • $rename Rename a column
    • $inc Grow a column

Db.stu.update ({name: ' Wukong '},{$set: {name: ' DZSF '}, $unset: {jingu:1}, $rename: {sex: ' Gendar '}, $inc: {age:1}}) #默认u Pdate only 1 lines.

Db.stu.update ({name: ' Wukong '},{$set: {name: ' DZSF '}, $unset: {jingu:1}, $rename: {sex: ' Gendar '}, $inc: {age:1}},{m Ulti:true})

#如果有这条记录就更新, if not, insert

Db.stu.update ({name: ' Bill '},{$set: {name: ' Bill ', Sex: ' M ', age:500}},{upsert:true})

#但是如果我要插入的字段不止这些, when inserting, you need to add more fields, using Setoninsert.

Db.stu.update ({name: ' Kevin '},{$set: {name: ' Bill ', Sex: ' M ', age:500}, $setOnInsert: {address: ' Shanghai '}},{upsert: True})

#第一部分查询表达式, the second part queries the column.

Db.stu.find ({},{name:1})

#id属性默认查出, if you don't want

Db.stu.find ({},{name:1, _id:0})

The basic crud for 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.