Node.js Mongodb按自定义id从会话集合查找结果
我使用的是Mongojs库,它是mongodb库的薄包装。我想在会话集合中执行一些查找查询,这意味着我使用mongo存储会话(连接mongo) 问题是Node.js Mongodb按自定义id从会话集合查找结果,node.js,mongodb,Node.js,Mongodb,我使用的是Mongojs库,它是mongodb库的薄包装。我想在会话集合中执行一些查找查询,这意味着我使用mongo存储会话(连接mongo) 问题是 db.sessions.find({}, cb); -- returns all the sessions 当我试图通过从req.sessionID-string中获取的id查找specify session时,它始终返回null db.sessions.findOne({ _id: req.sessionID }, cb); -- yes t
db.sessions.find({}, cb); -- returns all the sessions
当我试图通过从req.sessionID-string中获取的id查找specify session时,它始终返回null
db.sessions.findOne({ _id: req.sessionID }, cb); -- yes the seesionId exists, when i execute this query manually it works
还尝试直接连接“mongodb”库
var Db = require('mongodb').Db,
MongoClient = require('mongodb').MongoClient,
Server = require('mongodb').Server,
ReplSetServers = require('mongodb').ReplSetServers,
ObjectID = require('mongodb').ObjectID,
Binary = require('mongodb').Binary,
GridStore = require('mongodb').GridStore,
Code = require('mongodb').Code,
BSON = require('mongodb').pure().BSON,
assert = require('assert');
var db = new Db(session.db, new Server(session.host, session.port,
{auto_reconnect: false, poolSize: 4}), {w:0, native_parser: false});
function findBySid(sid, cb) {
db.close();
db.open(function(err, db) {
db.collection('sessions').findOne({ _id: sid }, function(err, session) {
cb(err, session);
});
})
结构本身看起来像这样,由connect mongo创建
> db.sessions.find({ _id: 'QFtHqaTdtg5kucwvBmY3BZ7m' }).pretty()
{
"_id" : "QFtHqaTdtg5kucwvBmY3BZ7m",
"session" : "{\"cookie\":{\"originalMaxAge\":null,\"expires\":null,\"httpOnly\":true,\"domain\":\"asad.\",\"path\":\"/\"}}",
"expires" : ISODate("2013-08-12T12:34:49.271Z")
}
实际上,Mongo文档另存为
{ "_id" : ObjectId("51ecdea131ce0986e06e91fe"),....}
所以你必须像下面这样找到答案
db.newcol.find({_id:ObjectId("51ecdea131ce0986e06e91fe")});
下面的shell输出将解释
>db.newcol.find({_id:51ecdea131ce0986e06e91fe});
Mon Jul 29 18:25:54.563 JavaScript execution failed: SyntaxError: Unexpected token ILLEGAL
> db.newcol.find({_id:"51ecdea131ce0986e06e91fe"}); //No output empty
> db.newcol.find({_id:ObjectId("51ecdea131ce0986e06e91fe")});
{ "_id" : ObjectId("51ecdea131ce0986e06e91fe"), "name" : "karthick.k", "email" : "karthdfgdf@gmail.com", "phone_no" : "6666666666" }
因此,请相应地构造查询。实际上,Mongo文档另存为
{ "_id" : ObjectId("51ecdea131ce0986e06e91fe"),....}
所以你必须像下面这样找到答案
db.newcol.find({_id:ObjectId("51ecdea131ce0986e06e91fe")});
下面的shell输出将解释
>db.newcol.find({_id:51ecdea131ce0986e06e91fe});
Mon Jul 29 18:25:54.563 JavaScript execution failed: SyntaxError: Unexpected token ILLEGAL
> db.newcol.find({_id:"51ecdea131ce0986e06e91fe"}); //No output empty
> db.newcol.find({_id:ObjectId("51ecdea131ce0986e06e91fe")});
{ "_id" : ObjectId("51ecdea131ce0986e06e91fe"), "name" : "karthick.k", "email" : "karthdfgdf@gmail.com", "phone_no" : "6666666666" }
因此,请相应地构造查询。尝试使用
.findOne({u id:new BSON.ObjectID(sid)}),
我不能使用ObjectID,因为会话id自定义字符串不是mongo生成的值尝试使用.findOne({u id:new BSON.ObjectID(sid)}),
我不能使用ObjectID,因为会话id自定义字符串不是mongo生成的值。您是正确的,但查看的源代码,您会发现_id是简单的会话id字符串添加了会话结构,并且了解了它在shellYes中的工作方式。因此,必须将sid
记录到console.log中,以交叉检查sid字符串是的,在其他地方存在问题,查找过程启动得太早,因为mongo没有填充会话数据。您是正确的,但是查看的源代码,您会发现_id是简单的会话id,添加了会话结构,并且理解了它在shellYes中的工作方式。因此,必须将sid
记录到console.log中,以交叉检查sid字符串。是的,在其他地方存在问题,因为mongo没有填充会话数据,所以查找过程开始得太早。