在MongoDB中使用函数/方法时访问权限不同

问题描述 投票:0回答:1

所有,

我创建了一个名为Insurance的数据库,并创建了一个名为iUser的用户,其读取角色为Insurance数据库,并在其中插入了一些文档。

我使用用户iUser连接到Mongo,我能够获取集合(ExposureFindings)并且能够获取其中的文档。

我做了以下工作来完成它:

C:\> mongo --username iUSer --password xyzpass --authenticationDatabase Insurance

> use Insurance
> db
Insurance
> show collections
ExposureFindings
ExposureProfiles
system.users
>
> db.ExposureFindings.find()
{ "_id" : ObjectId("5a83762d560a9792e3b060a4"), "ExposureFindings" : { "_id" : 254090, "CatCode" : "X36", "MainLocInd" : "Y", "ExcludeInd" : "E", "Exposure" : { "CatId" : 237075
2, "CatBasisCode" : "Lloyds823" } } }
{ "_id" : ObjectId("5a83762d560a9792e3b060a5"), "ExposureFindings" : { "_id" : 254091, "CatCode" : "A00", "MainLocInd" : "Y", "ExcludeInd" : "S", "Exposure" : { "CatId" : 240471
2, "CatBasisCode" : "Lloyds824" } } }
>

现在,如果我使用函数/方法尝试相同,那么它不会返回任何文档。以下是我的所作所为。

C:\> mongo
> conn = new Mongo()
> InsDB = conn.getDB("Insurance")
> InsDB.auth("iUser","xyzpass")
> InsDB
Insurance
> InsDB.getCollectionNames()
[ "ExposureFindings", "ExposureProfiles","system.users" ]
>  Coll = InsDB.getCollection("ExposureFindings")
Insurance.ExposureFindings
> Coll
Insurance.ExposureFindings
> InsDB.Coll.find()
> -- Returns no rows. However
> Insurance.ExposureFindings.find() -- return the below
{ "_id" : ObjectId("5a83762d560a9792e3b060a4"), "ExposureFindings" : { "_id" : 254090, "CatCode" : "X36", "MainLocInd" : "Y", "ExcludeInd" : "E", "Exposure" : { "CatId" : 237075
2, "CatBasisCode" : "Lloyds823" } } }
{ "_id" : ObjectId("5a83762d560a9792e3b060a5"), "ExposureFindings" : { "_id" : 254091, "CatCode" : "A00", "MainLocInd" : "Y", "ExcludeInd" : "S", "Exposure" : { "CatId" : 240471
2, "CatBasisCode" : "Lloyds824" } } }
>

使用InsDB.Coll.find()与db.ExposureFindings.find()或InsDB.ExposureFindings.find()相比,我做错了什么?

mongodb function call
1个回答
0
投票

这是因为Coll包含值Insurance.ExposureFindings。也就是说,Coll变量已经包含<Database>.<Collection>组合。

> Coll
Insurance.ExposureFindings

> InsDB.Coll
Insurance.Coll

从上面的输出中可以看出,当你调用InsDB.Coll时,它正在寻找名为Coll的集合。由于收集Coll不存在,它什么都不返回。

证明:

> db
Insurance

> db.Coll.insert({_id:0})
WriteResult({ "nInserted" : 1 })

> InsDB.Coll.find()
{ "_id" : 0 }

要回答您的问题,我们假设该集合包含一个文档:

> use Insurance
switched to db Insurance

> db.ExposureFindings.find()
{ "_id" : 1 }

你想要的是:

> InsDB.ExposureFindings.find()
{ "_id" : 1 }

要么:

Coll.find()
{ "_id" : 1 }
© www.soinside.com 2019 - 2024. All rights reserved.