当前位置:Gxlcms > mysql > mongodb数据插入

mongodb数据插入

时间:2021-07-01 10:21:17 帮助过:48人阅读

本节主要来说说如何将数据插入到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"
        ]
}
输入集合中:
> db.test_ttlsa_com.insert(document)
该操作会给文档自动添加一个"_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"]})
查看数据:
> 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"
                ]
        }
]
查看"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"
                ]
        }
]

人气教程排行