码迷,mamicode.com
首页 > 数据库 > 详细

MongoDB的简单实用-插入操作

时间:2018-03-11 12:08:49      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:方法   post   uid   track   insert   机器   mongo   进程   auth   

插入数据:

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")
}

MongoDB的简单实用-插入操作

标签:方法   post   uid   track   insert   机器   mongo   进程   auth   

原文地址:https://www.cnblogs.com/chzhang1994/p/8543120.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!