我一直在寻找mongodb中意外关闭的连接,但只能找到想要关闭它们的人的问题.
我使用node-mongodb-native连接到数据库,但我一直看似随机的"错误:连接关闭"消息.如果我手动重试请求(浏览器刷新),请求将起作用.
知道是什么导致了这个吗?有一些简单的选择会有所帮助吗?
我使用以下方法获取数据库句柄:
MongoClient.connect(connection_string, { auto_reconnect: true }, function (err, db) {
//server code/routes in here
}
Run Code Online (Sandbox Code Playgroud)
我正在浏览https://github.com/mongodb/node-mongodb-native/blob/master/lib/mongodb/connection/server.js,但我意识到我对如何管理连接池的有限理解正在绊倒我起来.我的印象是他们会在我服务器的生命周期内保持开放状态.有人可以帮忙吗?
编辑:在阅读了mjhm的评论之后,我开始更深入地了解TCP保持活力.偶然发现一些网站暗示这可能是Azure正在做的(这个问题现在被错误分类了!).显然,Azure负载均衡器会在1分钟的活动后终止连接.我正在使用Azure网站,因此它可能适用也可能不适用,但我认为这种洞察力很有希望开始新的调查.更多细节http://blogs.msdn.com/b/avkashchauhan/archive/2011/11/12/windows-azure-load-balancer-timeout-details.aspx
我有一个具有以下文档结构的数据库:
{
"_id" : ObjectId("520bea012ab230549e749cff"),
"Day" : 1,
"Time" : 54,
"State" : "Vermont",
"Airport" : "BTV",
"Temperature" : 39,
"Humidity" : 57,
"Wind Speed" : 6,
"Wind Direction" : 170,
"Station Pressure" : 29.6,
"Sea Level Pressure" : 150
}
Run Code Online (Sandbox Code Playgroud)
我需要为每个"州"找到最高的"温度"(例如,有100个带有"州"的文件:'佛蒙特州')并添加条目'month_high':true为此文件(具有最高温度)
这是我的代码:http://pastebin.com/UbACLbSF
但是当我在shell中运行程序时,我收到以下错误:
MongoError:无法规范化查询:BadValue错误的顺序数组[2]
对于我的应用程序在 MongoDB 上执行的每个操作,我希望拥有文档的旧版本和新版本,以便我可以发出两个版本的事件:
{
type: 'UPDATE',
before: documentBeforeUpdate,
after: documentAfterUpdate
}
Run Code Online (Sandbox Code Playgroud)
我现在执行此操作的方法是首先发出 afindOne查询,然后执行 afindOneAndUpdate更新,但使用文档_id进行查询。因此,如果查询实际上引起了数据库的负载,我不会付出两次这个代价:
async function updateOne(query, updates) {
const oldDocument = await this.model
.findOne(query, null, { lean: true })
.exec();
if (!oldDocument) {
return;
}
const newDocument = await this.model
.findOneAndUpdate({ _id: oldDocument._id }, updates, {
new: true,
lean: true
})
.exec();
// document vanished before it could be updated
if (!newDocument) {
return;
}
await this.emit("UPDATE", {
before: oldDocument,
after: newDocument,
type: …Run Code Online (Sandbox Code Playgroud) 我正在使用MongoDB和node-mongodb-native驱动程序.
我正在尝试返回具有不同属性的所有记录.
这似乎有效,但它只返回我正在检查的值是不同的值,而不是每个文档中的所有值.
这就是我试图只返回名称字段,我也试过没有它和变体,但它总是只返回数组中的item_id.
this.collection.distinct("item_id", [{"name" : true}, {sold : {"$exists" : true}}], function(err, results) {
if (err) {
callback(err);
} else {
console.log(results);
}
});
Run Code Online (Sandbox Code Playgroud)
有关如何从每个文档获取所有数据的任何建议吗?
谢谢!
编辑:使用Map Reduce
所以,我只是使用node-mongodb-native来设置map reduce的开始,这是我到目前为止所拥有的:
var map = function() {
emit(this._id, {"_id" : this._id, "name" : this.name});
}
var reduce = function(key, values) {
var items = [];
values.forEach(function(v) {
items.push(v);
});
return {"items" : items};
}
this.collection.mapReduce(map, reduce, {out: "res"}, function(err, results) {
if (err) {
console.log(err);
} else {
console.log(results);
} …Run Code Online (Sandbox Code Playgroud) 我使用node.js和node-mongodb-native驱动程序,以及连接池。有什么方法可以启用调试以查看发生了什么、有多少连接处于活动状态以及连接何时打开或关闭?
我想看到类似的东西:
* 连接 xxx 在主机上打开:端口 * 连接 yyy 在主机:端口上打开 * 连接 xxx 已关闭
我正在使用mongodb的node-mongodb-native驱动程序来编写一个网站.
我有一个关于如何打开mongodb连接一次的问题,然后在集合名称用户user.js和集合名称帖子中使用它comment.js
我想在db.js那时打开数据库连接,为用户和帖子集合插入/保存数据
目前代码,我的 db.js
var Db = require('mongodb').Db,
Connection = require('mongodb').Connection,
Server = require('mongodb').Server;
module.exports = new Db(
'blog',
new Server('localhost', Connection.DEFAULT_PORT, {auto_reconnect: true})
);
Run Code Online (Sandbox Code Playgroud)
我曾经db.js在user.js如下
var mongodb = require('./db');
function User(user){
this.name = user.name;
this.password = user.password;
this.email = user.email;
};
module.exports = User;
User.prototype.save = function(callback) {//save user information
//document to save in db
var user = {
name: this.name,
password: this.password,
email: this.email
};
mongodb.close();
//open mongodb …Run Code Online (Sandbox Code Playgroud) 我刚刚安装了Mongo,Node等,当我尝试通过我的nodejs服务器更新数据库时,我收到此错误:
MongoError: driver is incompatible with this server version
Run Code Online (Sandbox Code Playgroud)
以下是我的版本:
我有一切的最新版本,我搜索了节点mongodb驱动程序git,找出支持mongodb的版本,但我找不到任何东西:(
我还读了关于它的其他SO问题,它说要更新你的mongodb,但我的是最新版本!
有帮助吗?
我正在尝试使用"findAndModify"来实现"getOrCreate"行为.我正在使用本机驱动程序在nodejs中工作.
我有:
var matches = db.collection("matches");
matches.findAndModify({
//query
users: {
$all: [ userA_id, userB_id ]
},
lang: lang,
category_id: category_id
},
[[ "_id", "asc"]], //order
{
$setOnInsert: {
users: [userA_id, userB_id],
category_id: category_id,
lang: lang,
status: 0
}
},
{
new:true,
upsert:true
}, function(err, doc){
//Do something with doc
});
Run Code Online (Sandbox Code Playgroud)
我想要做的是:"找到与指定用户,lang和类别的特定匹配...如果找不到,插入一个具有指定数据的新用户"
Mongo抛出这个错误:
Error getting/creating match { [MongoError: exception: cannot infer query fields to set, path 'users' is matched twice]
name: 'MongoError',
message: 'exception: cannot infer query fields to set, path …Run Code Online (Sandbox Code Playgroud) 我正在使用 nodejs 客户端来循环输入并创建一个将传递给bulkWrite操作的值数组:
var updateVal = { sku: item.sku, name: item.name, updatedAt: new Date()};
var insertVal = { sku: item.sku, name: item.name, createdAt: new Date(), updatedAt: new Date()};
itemsToSave.push({
updateOne: {
filter: {
sku: item.sku
},
//update: { $set: updateVal }, // works
//update: { $setOnInsert: insertVal }, // works
update: { $set: updateVal, $setOnInsert: insertVal }, // DOES NOT work
upsert:true
}
});
Run Code Online (Sandbox Code Playgroud)
但由于某些原因,我不能混用$set和$setOnInsert......当我运行它,什么也没有发生,它就像一个无操作。这是批量操作的限制吗?
我正在使用 node-mongodb-native/2.1 来处理 mongodb 3.0.7
setOnInsert的文档表明这应该是可能的:
...
{ …Run Code Online (Sandbox Code Playgroud) 我对 MongoDB 比较陌生。起初我使用猫鼬,但现在我决定放弃它。我立即遇到了以下问题:我无法理解如何将所有执行的查询打印到控制台。
在猫鼬中,这可以像编写 mongoose.set('debug', true) 一样简单,但是如何使用本机驱动程序来做到这一点?
我在文档中阅读了有关 Logger 的内容,但输出对我来说似乎完全不可读。是否可以调整输出,或者我应该以某种方式解析它?