Node.js 使用mongodb查询sails项目中的嵌入式集合

Node.js 使用mongodb查询sails项目中的嵌入式集合,node.js,mongodb,sails.js,node-mongodb-native,sails-mongo,Node.js,Mongodb,Sails.js,Node Mongodb Native,Sails Mongo,我在项目中使用的是sails mongo,我需要在嵌入式集合中执行一个查询。 我的数据如下所示: { "_id" : ObjectId("53906c6254f36df504e99b8f"), "title" : "my post" "comments" : [ { "author" : "foo", "comment" : "foo comment" }, {

我在项目中使用的是sails mongo,我需要在嵌入式集合中执行一个查询。 我的数据如下所示:

{
    "_id" : ObjectId("53906c6254f36df504e99b8f"),
    "title"    : "my post"
    "comments" : [ 
        {
            "author" : "foo",
            "comment" : "foo comment"
        },
        {
            "author" : "bar",
            "comment" : "bar comment"
        }        
    ],
    "createdAt" : ISODate("2014-06-05T13:10:58.365Z"),
    "updatedAt" : ISODate("2014-06-05T13:10:58.365Z")
}
例如,我需要提取作者
foo

显然,sails还不支持此功能,因此我考虑使用的对象
db
进行此类查询。
由于sails mongo使用mongodb native,我可以访问我的sails项目中的db对象吗?或者我需要使用mongodb native建立一个新连接?
如果有人有更好的主意,我将不胜感激。谢谢

如果您只需要访问嵌入的评论,Waterline应该可以正常工作。只需执行一个普通的
find
findOne
,就可以在返回的对象上访问注释

如果您需要查询评论,例如要查找包含某作者评论的帖子,您可以使用Sails模型类的
.native()
方法访问底层的
mongodb native
集合:

Post.native(function(err, collection) {
    if (err) { 
        // handle error getting mongo collection
    }
    collection.find({'comments.author':'foo'}).toArray(function(err, results) {
        // Do something with results
    });
});
如果您只需要访问嵌入的注释,Waterline应该可以正常工作。只需执行一个普通的
find
findOne
,就可以在返回的对象上访问注释

如果您需要查询评论,例如要查找包含某作者评论的帖子,您可以使用Sails模型类的
.native()
方法访问底层的
mongodb native
集合:

Post.native(function(err, collection) {
    if (err) { 
        // handle error getting mongo collection
    }
    collection.find({'comments.author':'foo'}).toArray(function(err, results) {
        // Do something with results
    });
});
如果您只需要访问嵌入的注释,Waterline应该可以正常工作。只需执行一个普通的
find
findOne
,就可以在返回的对象上访问注释

如果您需要查询评论,例如要查找包含某作者评论的帖子,您可以使用Sails模型类的
.native()
方法访问底层的
mongodb native
集合:

Post.native(function(err, collection) {
    if (err) { 
        // handle error getting mongo collection
    }
    collection.find({'comments.author':'foo'}).toArray(function(err, results) {
        // Do something with results
    });
});
如果您只需要访问嵌入的注释,Waterline应该可以正常工作。只需执行一个普通的
find
findOne
,就可以在返回的对象上访问注释

如果您需要查询评论,例如要查找包含某作者评论的帖子,您可以使用Sails模型类的
.native()
方法访问底层的
mongodb native
集合:

Post.native(function(err, collection) {
    if (err) { 
        // handle error getting mongo collection
    }
    collection.find({'comments.author':'foo'}).toArray(function(err, results) {
        // Do something with results
    });
});