查询中的Mongo $可以在mongo中使用,但不能在sails中使用

时间:2022-07-07 18:16:22

Basically the query works in mongo but not in sails controller:

基本上查询在mongo中工作但在sails控制器中不起作用:

    db.membermodel.find({identifier:{$in:["2","3","4"]}); // works

    MemberModel.find({
        identifier:{$in:["2","3","4"]},
    }).then(function(members){
        // doesn't work
    });

data returned:

    { "_id" : ObjectId("52d1a484f2b5e88cb5d4072c"), "identifier" : "2", "deviceToken" : "token2"}
    { "_id" : ObjectId("52d1a487f2b5e88cb5d4072d"), "identifier" : "3", "deviceToken" : "token3"}

Thanks, Mars

2 个解决方案

#1


1  

This isn't the way to do in queries with Waterline. You simply set the attribute you're selecting to the array value:

这不是使用Waterline进行查询的方法。您只需将要选择的属性设置为数组值:

MemberModel.find({
    identifier:["2","3","4"]
}).exec(function(err, members){
    ...
});

If you really need to use low-level Mongo features, you can get an instance of the native collection with

如果您确实需要使用低级Mongo功能,则可以获取本机集合的实例

MemberModel.native(function(err, collection) {
   //do native mongo driver stuff with collection
}

#2


0  

Hard to understand how the model is being queried but I suggest you to "spy" what's Mongo getting as the MVC framework query, due to this is not a direct query to Mongo, it's passed through the framework. I'm quite sure you're still developing so you have access to your mongo instance, restart it using full profile (the trick is everything is slow under 1ms)

很难理解模型是如何被查询的,但我建议你“窥探”Mongo作为MVC框架查询得到的内容,因为这不是对Mongo的直接查询,而是通过框架传递。我很确定你还在开发中,所以你可以访问你的mongo实例,使用完整的配置文件重新启动它(诀窍是在1ms内一切都很慢)

mongod --profile=1 --slowms=1 &

mongod --profile = 1 --slowms = 1&

Tail the resulting log which normally is in

尾随通常所在的结果日志

/var/log/mongodb/mongodb.log

with the command

用命令

tail -f /var/log/mongodb/mongodb.log

tail -f /var/log/mongodb/mongodb.log

Send your query again and check what MongoDb is executing.

再次发送您的查询并检查MongoDb正在执行什么。

#1


1  

This isn't the way to do in queries with Waterline. You simply set the attribute you're selecting to the array value:

这不是使用Waterline进行查询的方法。您只需将要选择的属性设置为数组值:

MemberModel.find({
    identifier:["2","3","4"]
}).exec(function(err, members){
    ...
});

If you really need to use low-level Mongo features, you can get an instance of the native collection with

如果您确实需要使用低级Mongo功能,则可以获取本机集合的实例

MemberModel.native(function(err, collection) {
   //do native mongo driver stuff with collection
}

#2


0  

Hard to understand how the model is being queried but I suggest you to "spy" what's Mongo getting as the MVC framework query, due to this is not a direct query to Mongo, it's passed through the framework. I'm quite sure you're still developing so you have access to your mongo instance, restart it using full profile (the trick is everything is slow under 1ms)

很难理解模型是如何被查询的,但我建议你“窥探”Mongo作为MVC框架查询得到的内容,因为这不是对Mongo的直接查询,而是通过框架传递。我很确定你还在开发中,所以你可以访问你的mongo实例,使用完整的配置文件重新启动它(诀窍是在1ms内一切都很慢)

mongod --profile=1 --slowms=1 &

mongod --profile = 1 --slowms = 1&

Tail the resulting log which normally is in

尾随通常所在的结果日志

/var/log/mongodb/mongodb.log

with the command

用命令

tail -f /var/log/mongodb/mongodb.log

tail -f /var/log/mongodb/mongodb.log

Send your query again and check what MongoDb is executing.

再次发送您的查询并检查MongoDb正在执行什么。