我使用的是lokijs,它有“类似于mongo的”查询语言。
devices.insert({key:'d1', name:'Device1', users:['u1'], status: 'OK', current_wl:'123'})
devices.insert({key:'d2', name:'Device2', users:['u1','u1'], status: 'OK', current_wl:'123'})
devices.insert({key:'d3', name:'Device3', users:['u2','u3'], status: 'OK', current_wl:'123'})
devices.insert({key:'d4', name:'Device4', users:['u1','u2','u3','u4'], status: 'OK', current_wl:'123'})我尝试查找数组users中有用户'u1‘的设备时,返回了emty list:
a= devices.find( {users:{ "$in" : ["u1"] }} )
console.log("A", a);如果问题出在mongodb上,那么查询是否正确?在mongo中有没有其他的方法呢?在lokijs中有没有其他的方法呢?
发布于 2017-01-30 16:57:41
我不确定lokijs,但这是Mongo中的正确查询。
如果您只想查询在"users“数组中包含单个特定项的文档,那么在Mongo中,对于这种情况,一个更简单的查询应该是:
db.collection.find({ users: "u1" })发布于 2017-01-30 17:02:53
我找到了一个使用where的解决方案(或变通方法)
a= devices.where( function(obj){
return obj.users.indexOf('u1') > -1;
}
);https://stackoverflow.com/questions/41924906
复制相似问题