Mongodb Guide (translation) (24)-developer zone-insert object (1) Introduction

Source: Internet
Author: User

When we write data to MongoDB, they are always inserted in the document format. A document is a data structure similar to JSON, Python dictionary, and Ruby hash. Here, we will talk about documents and how to insert data into MongoDB.

Document-oriented

Document-oriented databases store "documents", but here documents refer to structured documents-the term may come from "XML documents ". Other structured data, such as JSON, and even embedded dictionaries in many languages, have similar properties.

Documents stored in MongoDB are JSON-like. JSON is a good way to store object-style data in a program. In a sense, it is language-independent and based on some standards.

To be more efficient, MongoDB uses a bson format to present data in binary format. Bson scans specified fields faster than JSON. Bson also adds some additional types, such as the date type and byte array type. Bson can be easily mapped to JSON or JSON, and can be mapped to multiple data structures in many programming languages.

The client driver serializes the data to bson and then transmits the data to the database through the line. Data is stored on the hard disk in bson format. In this way, when retrieving data, the database only needs to perform a small amount of conversions to send data, which shows high efficiency. The client driver deserializes the received bson object into the format of its local language.

JSON

For example, the following "document" can be stored in MongoDB:

{ author: 'joe',
created : new Date('03/28/2009'),
title : 'Yet another blog post',
text : 'Here is the text...',
tags : [ 'example', 'joe' ],
comments : [ { author: 'jim', comment: 'I disagree' },
{ author: 'nancy', comment: 'Good post' }
]
}

This document is a blog post, so we can use shell to store it in a "posts" collection:

> doc = { author : 'joe', created : new Date('03/28/2009'), ... }
> db.posts.insert(doc);

MongoDB understands the internal structure of bson objects-not only stores them, but also queries internal fields and creates indexes for these fields. For example, this query:

> db.posts.find( { "comments.author" : "jim" } )

It is possible that it means "querying all blog posts that contain a post prepared by jiim ".

Mongo friendly mode
Mongo can be used in many ways. The first instinct is that it is very similar to using relational databases. Mongo is designed to process rich content objects and can perform well.
Storage example

If you are creating an online store and using a relational database, your data structure may be similar:

item
title
price
sku
item_features
sku
feature_name
feature_value

You may organize the database structure in this way because different items have different feature values, and you do not want all possible feature values to be stored in a table. You can also implement the same data structure in mongo, but it may be easier in mongo:

item : {
"title" : <title> ,
"price" : <price> ,
"sku" : <sku> ,
"features" : {
"optical zoom" : <value> ,
...
}
}

This is awesome:

  • You can query the entire item at a time.
  • All the data of an item is stored in the same area on the disk, so that the disk can be searched.

At first glance, there seems to be some problems, but we have already considered them.

  • You may need to insert or update a separate feature. You can perform the following operations:

    • db.items.update( { sku : 123 } , { "$set" : { "features.zoom" : "5" } } )

        

  • Do I need to move the entire item on the disk to add a feature? No. Mongo reserves some space for each object to adapt to its growth. This also prevents index changes.
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.