Cấu trúc Document đơn giản

Ví dụ dưới đây minh họa cấu trúc Document của một Blog site với một cặp key-value phân biệt bởi dấu phảy.

{

    _id: ObjectId(7 df78ad8902c)

    title: 'MongoDB Overview',

    description: 'MongoDB is no sql database',

    by: 'tutorials point',

    url: 'http://www.tutorialspoint.com',

    tags: ['mongodb', 'database', 'NoSQL'],

    likes: 100,

    comments: [

        {

            user: 'user1',

            message: 'My first comment',

            dateCreated: new Date(2011, 1, 20, 2, 15),

            like: 0

        },

        {

            user: 'user2',

            message: 'My second comments',

            dateCreated: new Date(2011, 1, 25, 7, 45),

            like: 5

        }

    ]

}

Ở đây, _id là một số thập lục phân 12 byte để đảm bảo tính duy nhất của mỗi Document. Bạn có thể cung cấp _id trong khi chèn vào Document. Nếu bạn không cung cấp, thì MongoDB sẽ cung cấp một id duy nhất cho mỗi Document. Trong 12 byte này, 4 byte đầu là cho Timestamp hiện tại, 3 byte tiếp theo cho ID của thiết bị, 2 byte tiếp là process id của MongoDB Server và 3 byte còn lại là giá trị có thể tăng.

Last updated