Sim*_*mon 5 javascript native mongodb node.js capped-collections
我正在尝试使用Node.js(使用本机MongoDB驱动程序)在MongoDB中设置和更新一些上限集合.我的目标是,在运行app.js时,将文档插入到上限集合中,并更新上限集合中的现有文档.这两个都在运行setInterval(),所以每隔几秒钟.
我的问题:
db.createCollection("collectionName", { capped : true, size : 100000, max : 5000 } ).这将为我创建上限集合,但每次调用它时都会调用createCollection()而不是更新或插入 - 如果我调用createCollection(),一旦集合已经存在,它是否会完全覆盖现有集合?db.runCommand({"convertToCapped": "collectionName", size: 100000, max : 5000 });.这个问题是节点看不到runCommand()有效的函数而且它有错误.是否有其他东西我打算要求它让这个工作?它适用于mongo cli但不在节点内db.collections.find()一些查询,但在节点内它表明它find()不是有效的函数collection.update()向现有文档添加一些新字段?让我们说文档是一些简单的对象,{key1: "value", key2: "value"}但我有一个包含的对象{key3: "value"}.当前文档中不存在键3,如何将其添加到当前存在的内容中?这与上面的#4有些相关,因为我不确定要传递什么,因为查询参数find()似乎不能很好地与节点一起使用.关于您关于上限集合和自动创建它们的问题 1 - 4,有多种方法可以做到这一点。一方面,您可以运行一个脚本来初始化数据库,以便在您第一次运行它时为您的客户端提供可用的上限集合。另一方面,您可以在插入文档之前检查给定集合中是否有任何文档。如果有,您只需插入文档,如果没有,则创建上限集合,然后插入文档作为该函数的回调。它会像这样工作:
var host = "localhost",
port = 27017,
dbName = "so";
var MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server;
var mongoclient = new MongoClient(new Server(host, port));
var db = mongoclient.db(dbName);
db.open(function(err, db) {
if(err) throw err;
// Capped collection.
var capped = db.collection('capped');
// Document to be inserted.
var document = { "foo": 1, "bar": 1 }
capped.find().count(function(err, count) {
if(err) throw err;
if (count === 0) {
console.log("Creating collection...");
db.createCollection("capped",
{ "capped": true,
"size": 100000,
"max": 5000 },
function(err, collection) {
if(err) throw err;
// Insert a document here.
console.log("Inserting document...");
collection.insert(document, function(err, result) {
if (err) throw err;
});
});
} else {
// Insert your document here without creating collection.
console.log("Inserting document without creating collection...");
capped.insert(document, function(err, result) {
if (err) throw err;
});
}
});
});
Run Code Online (Sandbox Code Playgroud)
关于问题 5,您可以使用findOne()在集合中查找文档,尽管这不一定是第一个或最后一个文档。如果你想保证第一个或最后一个,你可以运行 afind()和 a sort()and limit()of 1。按_id升序排序应该会得到第一个文档。更多信息请参见此处。
// Sort 1 for ascending, -1 for descending.
capped.find().sort([["_id", 1]]).limit(1).nextObject(function(err, item) {
console.log(item);
});
Run Code Online (Sandbox Code Playgroud)
最后,对于问题 6,您只需将$set运算符与update()方法一起使用即可。更多信息请参见此处。
capped.update({ "foo": 1 }, { "$set": { "bar": 2 } }, {}, function(err, result) {
console.log(result);
});
Run Code Online (Sandbox Code Playgroud)
请注意,您只能更新上限集合的文档,因此您无法插入您提到的额外字段。这里列举了您可能需要了解的其他限制。
[编辑:在上一个文档中添加更新嵌套字段。]
如果要更新第一个或最后一个文档中的嵌套字段(分别在排序中使用 1 或 -1),您可以获取文档,提取 ,_id然后对该文档执行原子更新。像这样的东西:
capped.find().sort([["_id", -1]]).limit(1).nextObject(function(err, item) {
if(err) throw err;
capped.update({ "_id": item._id },
{ "$set": { "timeCollected": 15, "publicIP.ip" : "127.0.0.1" }},
function(err, result) {
if(err) throw err;
console.log(result);
});
});
Run Code Online (Sandbox Code Playgroud)
请注意,即使更新上限集合中文档中存在的字段,您也需要确保新值适合为文档分配的空间。因此,例如,将字符串值从 更新为"1"不一定"127.0.0.1"有效。
| 归档时间: |
|
| 查看次数: |
2097 次 |
| 最近记录: |