在 MongoDB 中使用 functions/methods 时访问权限不同
Access rights different when using functions/methods in MongoDB
全部,
我创建了一个名为 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" } } }
>
现在,如果我使用 Functions/Methods 尝试相同的操作,那么它不会返回任何文档。以下是我所做的。
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" } } }
>
与 db.ExposureFindings.find() 或 相比,我在使用 InsDB.Coll.find() 时做错了什么吗InsDB.ExposureFindings.find()?
这是因为 Coll
包含值 Insurance.ExposureFindings
。也就是说,Coll
变量已经包含一个 <Database>.<Collection>
组合。
> Coll
Insurance.ExposureFindings
> InsDB.Coll
Insurance.Coll
从上面的输出可以看出,当你调用InsDB.Coll
时,它正在寻找名为Coll
的集合。由于集合 Coll
不存在,因此 returns 什么都没有。
证明:
> 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 }
全部,
我创建了一个名为 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" } } }
>
现在,如果我使用 Functions/Methods 尝试相同的操作,那么它不会返回任何文档。以下是我所做的。
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" } } }
>
与 db.ExposureFindings.find() 或 相比,我在使用 InsDB.Coll.find() 时做错了什么吗InsDB.ExposureFindings.find()?
这是因为 Coll
包含值 Insurance.ExposureFindings
。也就是说,Coll
变量已经包含一个 <Database>.<Collection>
组合。
> Coll
Insurance.ExposureFindings
> InsDB.Coll
Insurance.Coll
从上面的输出可以看出,当你调用InsDB.Coll
时,它正在寻找名为Coll
的集合。由于集合 Coll
不存在,因此 returns 什么都没有。
证明:
> 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 }