MongoDB的简单实用-插入操作

插入数据:

MongoDB使用insertOne函数插入一条数据

如:

1.先用document指定插入的数据:

document = ({"Type":"Book","Title":"Definitive Guide to MongoDB 3rd ed.","ISBN":"978-1-4842-1183-0","Publisher":"Apress","Author":    ["Hows,David","Plugge,Eelco","Membrey,Peter","Hawkins,Tim"]})

系统返回

{
"Type" : "Book",
"Title" : "Definitive Guide to MongoDB 3rd ed.",
"ISBN" : "978-1-4842-1183-0",
"Publisher" : "Apress",
"Author" : [
"Hows,David",
"Plugge,Eelco",
"Membrey,Peter",
"Hawkins,Tim"
]
}

2.执行插入操作:

db.media.insertOne(document)

系统返回:

{
"acknowledged" : true,
"insertedId" : ObjectId("5aa47c1953350e04ddbd6fa3")
}

其中,insertedId是插入后形成的_id键值,保证唯一性,它由4位时间戳 3位机器id 2位进程id 3位计数器组成

 

还有一种方法直接通过shell插入数据

如:db.media.insertOne({"Type":"CD","Artist":"Nirvana","Title":"Nevermind","Tracklist":[{"Track":"1","Titie":"Smells Like Teen Spirit","Length":"5:02"},{"Track":"2","Title":"In Bloom","Length":"4:15"}]})

系统返回:

{
"acknowledged" : true,
"insertedId" : ObjectId("5aa47dd553350e04ddbd6fa4")
}

posted @ 2018-03-11 09:11  渡~solong  Views(401)  Comments(0Edit  收藏  举报