我试图找出如何显示所有文件用户可以访问,但我无论是在获得全部或不确定user.docs没有文件,如果人没有登录。如何查询用户在mongodb中有权访问的所有文档?
流星,我的用户有
docs: {
"xxx1",
"xxx2",
"xxx3"
}
等(也可能是IDS的无限数量。 如何查询蒙戈找到具有ID = user.docs所有文件?(文档返回数组)。
我试图找出如何显示所有文件用户可以访问,但我无论是在获得全部或不确定user.docs没有文件,如果人没有登录。如何查询用户在mongodb中有权访问的所有文档?
流星,我的用户有
docs: {
"xxx1",
"xxx2",
"xxx3"
}
等(也可能是IDS的无限数量。 如何查询蒙戈找到具有ID = user.docs所有文件?(文档返回数组)。
“如果一个人没有记录'?我假设这是服务器端?
你想抓住'文档'Meteor.user()吗? 只适用于当前登录的用户。在用户
服务器端的MongoDB查询:
Meteor.users.findOne({_id: '<userId string here>'},{fields: {docs: 1}});
应该给你:
{_id: '<userId string here>', docs: {...docs here...}}
小心用户数据!
编辑:
如果你想从一个用户对象匹配文档ID的文档,你会非常重新安排你的数据结构来
docs: ['docId', 'docId'] <---this may be your main problem
那么这将是:
var userDocsIds = Meteor.users.findOne({_id: '<userId string here>'},{fields: {docs: 1}});
var userDocs = Docs.find({_id: {$in: userDocsIds.docs}}).fetch();
使用$in
算子找到一个数组中的_ids
:
const docIds = Meteor.users.findOne(id).docs;
const myDocs = Docs.find({_id: {$in: docIds }});
我曾经尝试都和最终使用
docs = getDocsForUser(user); //print array of docs
return docs.find({_id: {$in: docs}})