MongoDB Quick Start Note (iii) MongoDB Insert Document Action _mongodb

Source: Internet
Author: User
Tags bulk insert mongodb

MongoDB is a database based on distributed file storage. Written by the C + + language. Designed to provide scalable, high-performance data storage solutions for WEB applications.

MongoDB is a product between relational database and non relational database, and is the most powerful and relational database in the relational database.

This article introduces MongoDB to the method of inserting the document, take a look together

1, the document data storage format is Bson, similar to JSON. MongoDB inserts data to verify that there is a "_id" in the data if it is not automatically generated.

Shell operations have insert and save two ways. When you insert a piece of data with a "_id" value, and you now have the same value in the collection, you insert it without inserting it, and when you use Save, the data is updated.

> Db.student.drop ()
true
> Db.student.insert ({"_id": 1, "name": "Zhangsan", "Age":})
Writeresult ({"ninserted": 1})
> Db.student.find ()
{"_id": 1, "name": "Zhangsan", "Age": {}
> Db.student.insert ({"_id": 1, "name": "En Angsan "," Age ":}"
Writeresult ({
"ninserted": 0,
"writeerror": {
"code": 11000,
"errmsg": " E11000 duplicate key error collection:zyhdb.student index: _id_ dup key: {: 1.0} "
}
})
> Db.student.f IND ()
{"_id": 1, "name": "Zhangsan", "Age": [}
> Db.student.save ({"_id": 1, "name": "Zhangsan", "Age": 2 7})
Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1})
> Db.student.find ()

2, BULK INSERT, Online documents are said not MongoDB does not support the bulk INSERT, now tried can, should be the current version of support for bulk inserts.

> Db.student.insert ([{"_id": 2, "name": "Lisi"},{"_id": 3, "name": "Wangwu"}, {"_id": 4, "name": "Zhaoliu", "Age": 28}] )
Bulkwriteresult ({
"writeerrors": [],
"writeconcernerrors": [],
"ninserted": 3,
"nupserted": 0,
"nmatched": 0,
"nmodified": 0,
"nremoved": 0,
"upserted": []
})
> Db.student.find ()
{"_id": 1, "name": "Zhangsan", "Age":}
{"_id":, "name": "Lisi"}
{"_id":, "name": "Wangwu"}

3. Recycle insert:

> for (var i= i<; i++) {Db.fortest.insert ({num:i})}
Writeresult ({"ninserted":})
> Db.fortest.find ()
{"_id": ObjectId ("Eceadaeabab"), "num": 0}
{"_id": ObjectId ("Eceadaeabab"), "num": 1}
{"_id": ObjectId ("Eceadaeabab"), "num": 2}
{"_id": ObjectId ("Eceadaeabab"), "num": 3}
{"_id": ObjectId ("Eceadaeabab"), "num": 4}
{"_id": ObjectId ("Eceadaeababa"), "num": 5}
{"_id": ObjectId ("Eceadaeababb"), "num": 6}
{"_id": ObjectId ("Eceadaeababc"), "num": 7}
{"_id": ObjectId ("Eceadaeababd"), "num": 8}
{"_id": ObjectId ("Eceadaeababe"), "num": 9}

The above is a small set to introduce the MongoDB Quick Start notes (iii) MONGODB insert the relevant knowledge of the document, I hope to help you, more wonderful content, please pay attention to cloud Habitat community website!

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.