获取与一组键匹配的集合中的所有文档 - mongoDB

时间:2014-11-16 10:44:12

标签: mongodb mongodb-query

我想查找我的收藏集中与roleId匹配的所有用户,例如administratormodwriterroleId存储为相应的数组元素。

该集合如下所示:

{ "_id" : "P7xfcnhvQY4mupyRs", "createdAt" : ISODate("2014-11-16T10:37:24.371Z"), "roles" : { "administrator" : [  "Y2FZnfx9Zi4NR6J6e" ] }, "services" : { "password" : { "bcrypt" : "$2a$10$rWvLQkpiQW55Cp9aF37OtOipuApzM7WKAVTLz/B3HwVypFM4uJEJy" }, "resume" : { "loginTokens" : [  {       "when" : ISODate("2014-11-16T10:37:24.376Z"),   "hashedToken" : "ER4k6eLwKHczqTUE9/7wIs/ZRGyGEPmtknEYn4MH/eM=" } ] } }, "username" : "John" }
{ "_id" : "ic6sNco6HMJJAgnuh", "createdAt" : ISODate("2014-11-16T11:06:09.697Z"), "roles" : { "writer" : [  "Y2FZnfx9Zi4NR6J6e" ] }, "services" : { "password" : { "bcrypt" : "$2a$10$BRLXmsCBD504YdZgLikgr.nNEvnRVStiQwQKaSQlZlhM138mf8cK2" }, "resume" : { "loginTokens" : [ ] } }, "username" : "Harry" }
{ "_id" : "LYAcBy58iiuZSbYhk", "createdAt" : ISODate("2014-11-16T11:06:22.854Z"), "roles" : { "mod" : [  "Y2FZnfx9Zi4NR6J6e" ] }, "services" : { "password" : { "bcrypt" : "$2a$10$Kp7.uyN0dBrl7hRBDAh/wu4niffCXpO3r6EM292xSj2useDjtx28G" }, "resume" : { "loginTokens" : [         {       "when" : ISODate("2014-11-16T11:06:22.859Z"),   "hashedToken" : "1D0uSqV3fy/LanaheiMGZPrIFzI5O3VESDrFKbrDl8g=" } ] } }, "username" : "Peter" }

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:3)

如果我理解你的问题,这将是你的答案

db.user_details.find(
{
    $or : 
    [
        {'roles.administrator' : "Y2FZnfx9Zi4NR6J6e"},
        {'roles.mod' :  "Y2FZnfx9Zi4NR6J6e"},
        {'roles.writer' : "Y2FZnfx9Zi4NR6J6e"}
    ]
})