Home > Database > Mysql Tutorial > mongodb数据插入

mongodb数据插入

WBOY
Release: 2016-06-07 16:34:48
Original
1083 people have browsed it

本节主要来说说如何将数据插入到mongodb集合中。所有存储在集合中的数据都是BSON各式的。BSON是一种以二进制存储的json格式。当数据插入时,使用的驱动程序会将数据转成BSON格式,然后插入集合中。数据库解析BSON,检查是否包含"_id"键,并且集合文档不得超

本节主要来说说如何将数据插入到mongodb集合中。所有存储在集合中的数据都是BSON各式的。BSON是一种以二进制存储的json格式。 当数据插入时,使用的驱动程序会将数据转成BSON格式,然后插入集合中。数据库解析BSON,检查是否包含"_id"键,并且集合文档不得超过4MB,除此之外,不做任何其他的检查。如果要对插入的文档进行检查,可以在启动服务时候使用--objcheck参数,这样会在插入前先进行文档结构的有效性检测,当然啦,性能方面会有所降低的。 定义一个文档:
> use ttlsa_com
switched to db ttlsa_com
> document=({"user_id" : "ABCDBWN","password" :"ABCDBWN" ,"date_of_join" : "15/10/2010" ,
... "education" :"B.C.A." , "profession" : "DEVELOPER","interest" : "MUSIC",
... "community_name" :["MODERN MUSIC", "CLASSICAL MUSIC","WESTERN MUSIC"],
... "community_moder_id" : ["MR. BBB","MR. JJJ","MR MMM"],
... "community_members" : [500,200,1500],"friends_id" : ["MMM123","NNN123","OOO123"],
... "ban_friends_id" :["BAN123","BAN456","BAN789"]});
{
        "user_id" : "ABCDBWN",
        "password" : "ABCDBWN",
        "date_of_join" : "15/10/2010",
        "education" : "B.C.A.",
        "profession" : "DEVELOPER",
        "interest" : "MUSIC",
        "community_name" : [
                "MODERN MUSIC",
                "CLASSICAL MUSIC",
                "WESTERN MUSIC"
        ],
        "community_moder_id" : [
                "MR. BBB",
                "MR. JJJ",
                "MR MMM"
        ],
        "community_members" : [
                500,
                200,
                1500
        ],
        "friends_id" : [
                "MMM123",
                "NNN123",
                "OOO123"
        ],
        "ban_friends_id" : [
                "BAN123",
                "BAN456",
                "BAN789"
        ]
}
Copy after login
输入集合中:
> db.test_ttlsa_com.insert(document)
Copy after login
该操作会给文档自动添加一个"_id"键,然后将其保存到mongodb中。 也可以不预先定义文档直接插入:
> db.test_ttlsa_com.insert({"user_id" : "ABCDBWN","password" :"ABCDBWN" ,"date_of_join" : "15/10/2010" ,"education" :"B.C.A." , "profession" : "DEVELOPER","interest" : "MUSIC","community_name" :["MODERN MUSIC", "CLASSICAL MUSIC","WESTERN MUSIC"],"community_moder_id" : ["MR. BBB","MR. JJJ","MR MMM"],"community_members" : [500,200,1500],"friends_id" : ["MMM123","NNN123","OOO123"],"ban_friends_id" : ["BAN121113","BAN456aaa"]})
Copy after login
查看数据:
> db.test_ttlsa_com.find().toArray()
[
        {
                "_id" : ObjectId("535289514a8013f575543dd1"),
                "user_id" : "ABCDBWN",
                "password" : "ABCDBWN",
                "date_of_join" : "15/10/2010",
                "education" : "B.C.A.",
                "profession" : "DEVELOPER",
                "interest" : "MUSIC",
                "community_name" : [
                        "MODERN MUSIC",
                        "CLASSICAL MUSIC",
                        "WESTERN MUSIC"
                ],
                "community_moder_id" : [
                        "MR. BBB",
                        "MR. JJJ",
                        "MR MMM"
                ],
                "community_members" : [
                        500,
                        200,
                        1500
                ],
                "friends_id" : [
                        "MMM123",
                        "NNN123",
                        "OOO123"
                ],
                "ban_friends_id" : [
                        "BAN123",
                        "BAN456",
                        "BAN789"
                ]
        },
        {
                "_id" : ObjectId("53528ac94a8013f575543dd2"),
                "user_id" : "ABCDBWN",
                "password" : "ABCDBWN",
                "date_of_join" : "15/10/2010",
                "education" : "B.C.A.",
                "profession" : "DEVELOPER",
                "interest" : "MUSIC",
                "community_name" : [
                        "MODERN MUSIC",
                        "CLASSICAL MUSIC",
                        "WESTERN MUSIC"
                ],
                "community_moder_id" : [
                        "MR. BBB",
                        "MR. JJJ",
                        "MR MMM"
                ],
                "community_members" : [
                        500,
                        200,
                        1500
                ],
                "friends_id" : [
                        "MMM123",
                        "NNN123",
                        "OOO123"
                ],
                "ban_friends_id" : [
                        "BAN121113",
                        "BAN456aaa"
                ]
        }
]
Copy after login
查看"ban_friends_id":"BAN121113"的数据:
> db.test_ttlsa_com.find({"ban_friends_id":"BAN121113"}).toArray()
[
        {
                "_id" : ObjectId("53528b974a8013f575543dd3"),
                "user_id" : "ABCDBWN",
                "password" : "ABCDBWN",
                "date_of_join" : "15/10/2010",
                "education" : "B.C.A.",
                "profession" : "DEVELOPER",
                "interest" : "MUSIC",
                "community_name" : [
                        "MODERN MUSIC",
                        "CLASSICAL MUSIC",
                        "WESTERN MUSIC"
                ],
                "community_moder_id" : [
                        "MR. BBB",
                        "MR. JJJ",
                        "MR MMM"
                ],
                "community_members" : [
                        500,
                        200,
                        1500
                ],
                "friends_id" : [
                        "MMM123",
                        "NNN123",
                        "OOO123"
                ],
                "ban_friends_id" : [
                        "BAN121113",
                        "BAN456aaa"
                ]
        }
]
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template