当前位置:Gxlcms > 数据库问题 > MongoDB: The Definitive Guide [1]

MongoDB: The Definitive Guide [1]

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

collections = ["posts", "comments", "authors"] for (i in collections) { doStuff(db.blog[collections[i]]); }

2.6 数据类型

基本数据类型、数字、日期(new Date())、数组、内嵌文档等。

  • 尽量不要在Shell下覆盖整个文档(整数转化为浮点数)

_id和ObjectId

  • ObjectId是_id的默认类型,用12字节的存储空间

技术分享

  • 秒级别的唯一性,大致会按照插入的顺序排列,隐含了文档创建的时间
  • _id通常由客户端驱动程序完成

第三章 创建、更新及删除文档

基本的插入删除修改操作

db.foo.insert({"bar":"baz"})
db.users.remove()
db.mailing.list.remove({"opt-out":true})
start = time.time()
db.drop_collection("bar")

joe.relationships={"friends":joe.friends,"enimies":joe.enimies};
delete joe.friends;
delete joe.enimies;
db.users.update({"name":"joe"}, joe)

使用修改器$set, $inc, $ne, $push, $addToSet, $each

//添加或删除键值
db.users.update({"name":"joe"}, {"$set":{"favorite book":"war and peace"}})
db.users.update({"name":"joe"}, {"$unset":{"favorite book":1}})

//增减数值
db.games.update({"game":"pinball", "user":"joe"}, {"$inc":{"score":50}})

//向数组添加值
db.papers.update({"authors cited":{"$ne":"Richie"}}, {"$push":{"authors cited":"Richie"}})

//集合中增加值
db.users.update({"_id":ObjectId("xxxx")},{"$addToSet":{"emails":"xxx@126.com"}})
db.users.update({"_id":ObjectId("xxxx")},{"$addToSet":{"emails":{"$each":["xxx@126.com", "xxx@hotmail.com"]}}})

//集合中删除值
db.lists.insert("todo":["dishes","laundry","dry cleaning"])
db.lists.update({},{"$pull":{"todo":1}})   //从数组末尾删除
db.lists.update({},{"$pop":{"todo":-1}}) //从数组开头删除

//数组定位
db.blog.update({"post":post_id},{"$inc":{"comments.0.votes":1}})  //用下标
db.blog.update({"comments.author":"John"},{"$set":{"comments.$.author":"Jim"}}) //只替换第一个

 

第四章 查询

第五章 索引

第六章 聚合

第七章 进阶指南

第八章 管理

第九章 复制

第十章 分片

第十一章 应用举例

MongoDB: The Definitive Guide [1]

标签:

人气教程排行