- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
在顺序很重要的MongoDB中存储一组文档的好方法是什么?我需要轻松地将文档插入任意位置,并可能在以后重新排序。
我可以为每个项目分配一个递增的数字并以此排序,或者我可以按_id
进行排序,但是我不知道如何在另一个文档之间插入另一个文档。假设我想在sequence
为5
的元素和sequence
为6
的元素之间插入一些内容?
我的第一个猜测是增加以下所有元素的sequence
,以便使用诸如db.items.update({"sequence":{$gte:6}}, {$inc:{"sequence":1}})
之类的查询为新元素留出空间。我对数据库管理的有限了解告诉我,这样的查询会很慢,并且通常是一个坏主意,但我很高兴得到纠正。
我想我可以将新元素的sequence
设置为5.5
,但是我认为这会很快变得困惑。 (再次,如果我错了,请纠正我。)
我可以使用有上限的集合,该集合有保证的顺序,但是如果我需要增加集合的数量,那么我会遇到问题。 (再次,我可能也错了。)
我可以让每个文档都包含对下一个文档的引用,但这将需要查询列表中的每个项目。 (您将获得一个项目,将其插入结果数组,并基于当前项目的next
字段获得另一个项目。)除了明显的性能问题之外,我也无法将经过排序的mongo游标传递给我的{#each}
空格键块表达式,并随着数据库的变化而实时更新。 (我正在使用Meteor全栈javascript框架。)
我知道一切都有其优点和缺点,我可能只需要使用上面列出的选项之一,但是我想知道是否有更好的方法来做事情。
最佳答案
根据您的需求,一种方法可能是设计模式,以使每个文档都能够容纳一个以上的文档,并且其本身就可以作为一个有顶盖的容器。
{
"_id":Number,
"doc":Array
}
doc
字段中。
doc
字段是一个数组,将保持插入顺序。
n
。因此,每个容器文档的
_id
字段将增加
n
,指示容器文档可以容纳的文档数。
extra fields
添加到文档
extra indices
,
unnecessary sorts
中。
var record = {"name" : "first"};
db.col.insert({"_id":0,"doc":[record]});
_id
和的number
它拥有的文件。 n
,则更新container document
最多可以容纳
5
文档,我们想插入一个新文档。
var record = {"name" : "newlyAdded"};
// using aggregation, get the _id of the last inserted container, and the
// number of record it currently holds.
db.col.aggregate( [ {
$group : {
"_id" : null,
"max" : {
$max : "$_id"
},
"lastDocSize" : {
$last : "$doc"
}
}
}, {
$project : {
"currentMaxId" : "$max",
"capSize" : {
$size : "$lastDocSize"
},
"_id" : 0
}
// once obtained, check if you need to update the last container or
// create a new container and insert the document in it.
} ]).forEach( function(check) {
if (check.capSize < 5) {
print("updating");
// UPDATE
db.col.update( {
"_id" : check.currentMaxId
}, {
$push : {
"doc" : record
}
});
} else {
print("inserting");
//insert
db.col.insert( {
"_id" : check.currentMaxId + 5,
"doc" : [ record ]
});
}
})
aggregation
在服务器端运行,非常高效,还请注意,
aggregation
将返回
文档而不是
previous to 2.6
版本中的
游标。因此,您需要修改以上代码以仅从单个文档中进行选择,而不是迭代游标。
1
和
2
之间插入一个新文档,我们知道该文档应放入带有
_id=0
的容器内,并应放置在该容器的
second
数组中的
doc
位置。
$each
和
$position
运算符将其插入特定位置。
var record = {"name" : "insertInMiddle"};
db.col.update(
{
"_id" : 0
}, {
$push : {
"doc" : {
$each : [record],
$position : 1
}
}
}
);
overflowing
中的文件
container
,比如说我们在之间插入一个新文件
_id=0
到容器中。如果容器已经有
5
文档,则需要
move the last document to the next container
并这样做,直到所有容器都在其容量范围内容纳文档为止,如果需要,最后需要创建一个容器来容纳溢出的文档。
register
编码。
db.system.js.save( {
"_id" : "handleOverFlow",
"value" : function handleOverFlow(id) {
var currDocArr = db.col.find( {
"_id" : id
})[0].doc;
print(currDocArr);
var count = currDocArr.length;
var nextColId = id + 5;
// check if the collection size has exceeded
if (count <= 5)
return;
else {
// need to take the last doc and push it to the next capped
// container's array
print("updating collection: " + id);
var record = currDocArr.splice(currDocArr.length - 1, 1);
// update the next collection
db.col.update( {
"_id" : nextColId
}, {
$push : {
"doc" : {
$each : record,
$position : 0
}
}
});
// remove from original collection
db.col.update( {
"_id" : id
}, {
"doc" : currDocArr
});
// check overflow for the subsequent containers, recursively.
handleOverFlow(nextColId);
}
}
after every insertion in between
,我们可以通过传递容器ID
function
来调用此
handleOverFlow(containerId)
。
$unwind
中使用
aggregate pipeline
运算符即可。
db.col.aggregate([{$unwind:"$doc"},{$project:{"_id":0,"doc":1}}]);
.."doc":[{"_id":0,","name":"xyz",...}..]..
var docArray = db.col.find({"_id":0})[0];
docArray.sort( function(a, b) {
return a._id - b._id;
});
What's a good way to store a set of documents in MongoDB where order is important?I need to easily insert documents at an arbitrary position and possibly reorder them later.
Say I want to insert something between an element with a sequence of 5 and an element with a sequence of 6?
$each
函数中使用
$position
和
db.collection.update()
运算符。
My limited understanding of Database Administration tells me that a query like that would be slow and generally a bad idea, but I'm happy to be corrected.
I could use a capped collection, which has a guaranteed order, but then I'd run into issues if I needed to grow the collection. (Yet again, I might be wrong about that one too.)
关于mongodb - 如何在不使用封顶集合的情况下在MongoDB中存储一组有序文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26220879/
我已经在 kubernetes 中部署了一个 3 pod mongodb statefulset,并且我正在尝试使用新的 mongodb+srv 连接字符串 (mongodb 3.6) 连接到具有 S
我已经创建了 MongoDB Atlas 帐户,并尝试连接。但出现以下错误。 MongoDB 连接错误 MongoNetworkError: 首次连接时无法连接到服务器 [cluster0-shard
我正在使用 Node-WebKit 创建桌面应用程序。该应用程序基本上是创建文档(员工日常工作的详细信息),任何注册用户都可以对这些文档发表评论。我正在创建的文档将被分成几个部分。用户将对特定部分发表
我正在尝试使用官方网站上的安装程序在我的本地机器上安装 mongo DB。但是我不断收到这条消息,有人可以帮忙吗? 我试过提供的解决方案 here但没有帮助。 最佳答案 我建议执行以下操作: 按 Wi
我对 MongoDB 和 MongoDB Compass 非常陌生。 我的客户集合中有大约 1000 条记录。如何通过 MongoDB 指南针一次删除所有记录。 非常感谢, 最佳答案 您可以使用 Mo
当我尝试在我的 Ubuntu 机器中安装 mongodb 时,apt-get 会显示以下选项 mongodb mongodb-clients mongodb-dev mongodb-server 谁能
如何将 Robomongo(或任何其他 mongodb 客户端)连接到由本地 Meteor 应用程序创建的 mongodb 实例? 最佳答案 确保 Meteor 正在本地主机上运行。打开终端窗口并运行
我需要在 MongoDB 中生成一个简单的频率表。假设我在名为 books 的集合中有以下文档。 { "_id": 1, genre: [ "Fantasy", "Crime"
我如何在 mongos mapreduce 中指定一个条件,就像我们在 mongos group 函数中所做的那样。 我的数据是这样的 {lid:1000, age:23}, {lid:3000, a
我的 mongodb 数据库文档中有几个 ID。我需要通过脚本在这些 ID 上创建索引,这样我就不必一次又一次地运行 ensureIndex 命令。 db.getCollection("element
在我的数据库中,每个包含项目的文档中都有一个嵌套的元素数组,格式如下: elements:[ { "elem_id": 12, items: [ {"i_id": 1
我正在构建一个应用程序,其中用户可以位于不同的时区,并且我运行的查询对他们的时区很敏感。 我遇到的问题是 MongoDB 似乎在查询时忽略了时区! 这是日期字段“2019-09-29T23:52:13
我正在研究使用 mongodb 进行分片,我有以下结构: 1 个 Mongod 到我的 ConfigServer,在 ReplicaSet 中只有 1 个成员 2 个分片,每个分片在 ReplicaS
我正在尝试获取一个 mongoDB 对象,例如 Friend1 包含另一个 mongoDB 对象 Friend2,该对象又包含第一个对象 Friend1本质上使它成为一个循环对象引用。 要么这样,要么
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题? Update the question所以它是on-topic对于堆栈溢出。 9年前关闭。 Improve this que
Mongo 版本 5.0.2。 Ubuntu 20.0 我在本地主机中启用了 MongoDB 连接的安全性。 我正在尝试通过以下命令使用身份验证详细信息连接我的本地主机 MongoDBmongo ad
我即将将分片的 MongoDB 环境从 2.0.7 升级到 2.2.9,最终我想升级到 2.4.9,但显然我需要通过 2.2 来完成。 2.2 的发行说明声明配置服务器应该首先升级其二进制文件,然后是
目前,我无法在我的虚拟 Ubuntu 机器上远程连接 mongodb 服务器。我无法使用在我的 Windows PC 上运行的 Robomongo 客户端连接,该 PC 也运行 vm。 这是两台电脑的
我创建了一个免费的 mongodb 集群。我创建了一个用户,设置了与 mongodb compass 的连接,复制了连接字符串,然后打开了我的 mongodb compass。将复制的字符串粘贴到那里
我使用 java 代码创建了 mongo 数据库集合索引 dbCollection.createIndex("accountNumber"); 当我看到索引使用 db.accounts.getInde
我是一名优秀的程序员,十分优秀!