我想通过node-mongodb-native驱动程序在我的一个MongoDB数据库上启用分析.
但是,似乎没有一种Db.setProfilingLevel()方法(除了Admin DB之外).
我试过用db.command({setProfilingLevel: 2})但得到了no such cmd: setProfilingLevel.
通过mongo shell可以正常工作 db.setProfilingLevel(2)
我连接到我的数据库,如下所示:
var mongoClient = new MongoClient(new Server('localhost', 27017, {auto_reconnect: true}));
mongoClient.open(function (err, mongoClient) {
var db = mongoClient.db('db_name');
db.authenticate('user', 'password', function () {err, result} {
if (err || !result) return console.log('error');
app.use(express.session({
store: new MongoStore({
db: db
})
});
});
});
Run Code Online (Sandbox Code Playgroud)
我想与MongoStore共享数据库连接,但它似乎不起作用.我该怎么办?
编辑:我在我的数据库上使用身份验证,但在新的MongoStore()获取执行后,我收到以下错误:
not authorized for query on site.system.indexes
Run Code Online (Sandbox Code Playgroud) 我的文档结构与以下内容大致相似:
{
"_id": "theIdOfThisObject",
"subdoc": {
"array": [
[
{
"parameters": {},
"winner": null,
"participants": [
"Person1",
"Person2"
]
},
{
"parameters": {},
"winner": null,
"participants": [
"Person3",
"Person4"
]
}
],
[]
]
},
}
Run Code Online (Sandbox Code Playgroud)
我完全试图替换嵌套数组中的一个子文档.我不需要搜索它 - 我知道确切的位置.
例如,我试图用第一个子文档替换
"parameters": {"frog":20},
"winner": "Person1",
"participants": [
"Person1",
"Person2"
]
Run Code Online (Sandbox Code Playgroud)
我们会说它被保存为一个叫做的对象newObject.
我希望以下代码可以工作(并且Model是一个真正的Mongoose模型):
Model.findByIdAndUpdate('theIdOfThisObject',{$set: {'subdoc.array.0.0':newObject}}, function(err,doc){
console.log('doc is returned, but nothing is updated')
});
Run Code Online (Sandbox Code Playgroud)
我不知道发生了什么,为什么这不起作用.有人有什么建议吗?我一直在使用MongoDB的本机节点驱动程序很长一段时间(3年),但是Mongoose对我来说还是比较新的.
编辑 - 根据评论请求添加模式
架构非常简单.看起来如下:
var Schema = new mongoose.Schema({
subdoc: {
array: [{}] …Run Code Online (Sandbox Code Playgroud) 我有一个处理mongodb数据库的开源项目.我正在尝试创建一个查询数据库以检查条目是否存在的函数.
问题是当if_exists()返回true或false时它返回undefined,因为mongodb驱动程序函数是异步的.该文件是Query.js,我在这里尝试了解决问题的方法在Node.js中进行同步MongoDB查询的正确方法是什么?但是我仍然使用get方法得到一个未定义的结果.
使这项工作的最佳方法是什么?
单元测试的输出如下:
running unit tests...
add query test
exists tests:
get: undefined
{}
should be true: undefined
get: undefined
{}
should be false:undefined
Captains Logs listening on port 3000
Captains_Logs v0.5.0-21
[ { name: 'rhcp', _id: 50cbdcbe9c3cf97203000002 } ]
[ { name: 'os', _id: 50cbdcbe9c3cf97203000001 } ]
Run Code Online (Sandbox Code Playgroud)
您可以在WeaponXI/cplog中浏览整个代码
或者快速查看query.js代码是:
var DB = require('../../lib/db.js').DB;
function methods() {
//query object
var Q = {};
//will act as our private variables to workaround …Run Code Online (Sandbox Code Playgroud) 我正在尝试在NodeJS中实现以下MongoDB查询
db.tvseries.find({}).map(function(doc){
var userHasSubscribed = false;
doc.followers && doc.followers.forEach(function(follower) {
if(follower.$id == "abc") {
userHasSubscribed = true;
}
});
var followers = doc.followers && doc.followers.map(function(follower) {
var followerObj;
db[follower.$ref].find({
"_id" : follower.$id
}).map(function(userObj) {
followerObj = userObj;
});
return followerObj;
});
return {
"id": doc.name,
"userHasSubscribed": userHasSubscribed,
"followers": followers || []
};
})
Run Code Online (Sandbox Code Playgroud)
以下是数据库
用户集合
{
"id": ObjectId("abc"),
"name": "abc_name"
},
{
"id": ObjectId("def"),
"name": "def_name"
},
{
"id": ObjectId("ijk"),
"name": "ijk_name"
}
Run Code Online (Sandbox Code Playgroud)
电视连续剧集
{
"id": ObjectId("123"),
"name": "123_name",
"followers": …Run Code Online (Sandbox Code Playgroud) 我正在尝试查询以下文档,并希望在"TaxonomyIDs"字段中列出包含TaxonomyID"1"的所有文档.
...
"Slug" : "videosecu-600tvl-outdoor-security-surveillance",
"Category" : "Digital Cameras",
"SubCategory" : "Surveillance Cameras",
"Segment" : "",
"Usabilities" : [
"Dome Cameras",
"Night Vision"
],
"TaxonomyIDs" : [
1,
12,
20,
21,
13
],
"Brand" : "VideoSecu",
...
Run Code Online (Sandbox Code Playgroud)
完全卡住了!
如果我调用findAndModify,并且文档与查询匹配,但现有文档包含文档没有的字段,旧文档是否仍然存在于新文档中,还是会被删除?
我检查了findAndModify文档,答案并不明显.尝试一下,旧文档中不在新文档中的字段似乎被删除 - 因为这涉及丢失数据,并且没有任何明确的关于这是文档,我不确定这是否是预期的行为.
我是node.js和MongoDB的新手,所以请耐心等待.我有一个表格,可以将细节存储到MongoDB集合中; 然后我使用.findOne(现在)查询.我基本上试图将此值传递到我的index.ejs文件中并将其显示为表的一部分.所以表格应该出现:
名称:Web浏览器中的Connor.
我查询数据库的代码:
router.get('/', function(req, res) {
res.render('admin/index');
MongoClient.connect("mongodb://localhost:27017/tickets", function(err, db) {
// Ensure we have connected
if(err) {
console.log("Cannot connect to database");
} else {
console.log("Connected to database");
}
// Create a collection to query
var collection = db.collection('tickets');
collection.findOne({name:String}, function(err, item) {
// Ensure we have found the ticket
if(err) {
console.log("There was a problem finding the ticket.");
} else {
console.log("Ticket found!");
}
});
});
});
Run Code Online (Sandbox Code Playgroud)
我的代码生成表:
<table>
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Subject</th>
<th>Message</th>
</tr>
</thead> …Run Code Online (Sandbox Code Playgroud)