4. MongoDB Insert, modify, delete operation

Source: Internet
Author: User
Tags mongodb update

    • MongoDB insert Operation

Syntax: Db.collection.save (collections); or Db.collection.insert (collections);

There are two ways of inserting.

1. Borrowing objects to insert

>item={id:1,name: "Hello"}>db.lottu.insert (item)

2. Insert data in a loop; Insert 5 records at once this is pretty good.

>for (i=1;i<=5;i++) Db.lottu.insert ({id:i,name: "KK"})

3. View data

    • MongoDB Modify Operation

Syntax: Db.collection.update (c1, $set, Multi,upsert)

1. Update a record
Update id=2 's record name is Lottu
>update Lottu Set name = ' Lottu ' where id=2

Then I was naughty; I wanted to change id=1 's record to Lottu.

The strange thing is: Why only one record is updated.
2. Update multiple records
Here we look for solutions. In MongoDB update multiple strips need to add multi (many) this parameter is True
>db.lottu.update ({id:1},{$set: {name: "li0924"},{multi:true}})

The update was successful. I don't think I lied to you.
Summary: Do not add multi parameters; By default, only one is updated. Add {multi:true} to update more than one
3. Update records that do not exist.
Update records that do not exist in Oracle, and the records do not change. Where's MongoDB?
Update records for ID=10

This, of course, does not change in MongoDB. No change is I didn't add upsert parameter; Now I add the next

This seems to have no record; This is somewhat similar to the merge into statement. But the concept is not the same.
Summary: {upsert:true}, this record is not added.

    • MongoDB Delete operation

Syntax: Db.collecion.remove (C1)
Here, remove is equivalent to the DELETE statement in SQL
For example, delete a record with an ID of 10;id 5
>delete from Lottu where id = 10;
>delete from Lottu where id = 5;

This is nothing to explain.
>delete from Lottu? Delete all records?
Is it Db.lottu.remove ()?

The result is: Db.lottu.remove ({})
We know that delete can delete records, but we do not delete the table. This, of course, is the same in MongoDB.
The object is deleted only on the drop table.
And what about the drop in MongoDB?

4. MongoDB Insert, modify, delete operation

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.