插入数据:
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")
}
原文地址:https://www.cnblogs.com/chzhang1994/p/8543120.html