MongoDB Increase and deletion check (a)

Source: Internet
Author: User
Tags modifier modifiers

This article mainly introduces MongoDB database additions and deletions to check operation.

Increase

MongoDB, like other relational databases, uses insert to add data to the collection.

db.collectionName.insert(内容)

To display all the collections in the database:

show collections

Delete

In MongoDB, remove removes documents that meet certain criteria in the collection.
Remove accepts a parameter as a condition to find the document to delete:

Of course, you can also delete an entire collection directly by dropping the method:

db.person.drop()

Deleting a collection and then rebuilding the index is faster than deleting all the documents in the collection.

Change

The modification operation is more complicated than the addition and deletion, because MongoDB can not only use the Update method, but also can use a lot of auxiliary modifiers, we first say the Update method.

Update

The Update method accepts two parameters, the first of which is to find the qualification of the document, and the second new document that needs to be modified:

The above update ({“name”:”liufang”},post) , in the equivalent of the name:liufang relational database in the where in the judgment condition, and post is relative to the SET statement after the execution content.

The simplest thing to update is to use a new document instead of a matching document, which is suitable for a time when the pattern structure has changed significantly. As in the following document:

{  "name":"tyq",  "age":22,  “date”:newDate()}

Modified to:

{  "name":"tyq",    “age”:22,    “friends”:”liufang”}

Such as:

modifier

Let's talk about MongoDB's powerfulmodifier
MongoDB has some auxiliarymodifierLike what inc, Set u n s Span class= "Mi" id= "mathjax-span-1075" style= "Font-family:mathjax_math; Font-style:italic; " >e t unset p u s Span class= "Mi" id= "mathjax-span-1083" style= "Font-family:mathjax_math; Font-style:italic; " >h Pop ad d T oseT, Each and so on. Here we introduce:

$inc

$inc used to increase and decrease the key or value.
When it is used to increase the value of an existing key, the key is incremented if there is no key. Usually used for analyzing data, polling, etc. location. Such as:

db.person.insert(  {"url":"blog.csdn.net/mevicky"})

Use $inc to add a key pageviews, the default value is 10000

db.person.update(  {"url":"blog.csdn.net/mevicky"},  {"$inc":{"pageViews":10000}})

Use $inc to give the key pageviews and add 10000

db.person.update(  {"url":"blog.csdn.net/mevicky"},  {"$inc":{"pageViews":10000}})

You can also use $inc to reduce the key pageviews by 10000

db.person.update(  {"url":"blog.csdn.net/mevicky"},  {"$inc":{"pageViews":-10000}})

Examples are as follows:

$set

$set is used to specify a value for a key that is created if the key does not exist, and is typically used to update a value or to add a newly defined key. Such as:

db.person.insert(  {    "name":"lf",    "age":23,    "sex":"male"  })

Add your favorite books:

db.person.update(  {    "name":"lf"  },  {    "$set":{"book":"war and peace"}  })

Modify your favorite books:

db.person.update(  {    "name":"lf"  },  {    "$set":{"book":"war and peace2"}  })

Examples are as follows:

$unset

$unset is used to delete the key and will not error if it is not found.

db.person.update(  {    "name":"lf"  },  {    "$unset":{"book":1}  })
$push

< Span class= "Mrow" id= "mathjax-span-1403" >p u s Span class= "Mi" id= "mathjax-span-1407" style= "Font-family:mathjax_math; Font-style:italic; " >h and A pop can only be used in an array type, and if the specified key already exists, $push adds an element to the end of the existing array and creates a new array if the key does not exist.
For example, to the above document, add a "comment" key that contains an array, and push a comment to the comment array.
This array is automatically created and added to the comments:

db.person.update(  {"name":"lf"},  {     $push:    {      "comments":      {        "name":"tyq",        "content":"nice"      }    }  })

Examples are as follows:

$pop

p o p Span class= "Texatom" id= "mathjax-span-1576" > and Push is similar, except that it deletes an element from an array, which can delete an element from either end of the array:

{$pop:{key:1}}//从数组末尾删除一个元素{$pop:{key:-1}}//从数组头部删除一个元素
$pull

$pull can delete elements based on specific conditions you specify, or you can delete elements based on their location:

db.person.update(  {"name":"lf"},  {    "$pull":    {      "emails":"362512489@qq.com"    }  })

$pull will delete all the matching parts, such as the array "1,2,3,4,4,4", after the pull 4 is executed, the resulting array is "all-in-one"

$addToSet

$addToSet is used to add data to an array, and is not repeated if there is one in the array.

$each

$each used to run a modifier to manipulate data multiple times to a collection, you can use $addToSet and $each add different values together:

db.users.update(  {"name":"lf"},  {    "$addToSet":    {      "emails":      {        "$each":        [          "362512489@111.com",          "362512489@112.com",          "362512489@113.com"        ]      }    }  })

Only a subset of modifiers are described here, and if you want to know all the modifiers you can check the official documentation:
Introduction to Modifiers

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

MongoDB Increase and deletion check (a)

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.