【发布时间】:2021-05-09 11:36:23
【问题描述】:
我刚开始在我的项目中使用猫鼬库,但是出现了一个大问题。 我在数据库中有很多集合,当我在其中一些集合中使用 find 或 aggregate 时效果很好,但在其他集合中却不行:
const mongoose=require('mongoose')
modelo = new mongoose.model('usuarios', new mongoose.Schema({}));
await mongoose.connect(uri, { useNewUrlParser:true, useUnifiedTopology: true })
modelo.find().then((data)=>{
console.log(data) // It has data with some collections, with others [] even though it has data
})
当我用另一个名称更改集合名称时,它不起作用,它是同一个数据库,它适用于我 10 个集合中的 5 个。如果一个集合返回数据,它总是返回数据,其他的我删除、重新创建,什么都没有。
显而易见的问题:它们存在,它们有数据,它们是以相同的方式创建的。
用户拥有所有数据库的读/写权限
我还尝试将模式与所有字段一起使用,并且答案相同(我想象的许多组合也可以使用)。
现在,当我使用:
mongoose.connection.db.collection(collectionName,async (err,actualColection)=>{
const xx=await actualColeccion.find().toArray()
console.log(xx) // yeah !!! it returns data
})
它工作正常,它返回数据。
问题:
- 这是猫鼬错误吗?
- 有没有更好的方法在 mongo 数据库中查找数据?
- 有比猫鼬更好的包吗?
提前感谢您的帮助。
pd: listCollections 返回:
[
{
name: 'menues', // returns data
type: 'collection',
options: {},
info: { readOnly: false, uuid: [Binary] },
idIndex: { v: 2, key: [Object], name: '_id_' }
},
{
name: 'servicios', // returns data
type: 'collection',
options: {},
info: { readOnly: false, uuid: [Binary] },
idIndex: { v: 2, key: [Object], name: '_id_', ns: 'suiteMBC.servicios' }
},
{
name: 'usuariosPalm', // returns [] but it has data even though it has ns ( namespace I suppose)
type: 'collection',
options: {},
info: { readOnly: false, uuid: [Binary] },
idIndex: { v: 2, key: [Object], name: '_id_', ns: 'suiteMBC.usuariosPalm' }
},
{
name: 'contratos', // returns data
type: 'collection',
options: {},
info: { readOnly: false, uuid: [Binary] },
idIndex: { v: 2, key: [Object], name: '_id_' }
},
{
name: 'maepro', // returns [] but it has data
type: 'collection',
options: {},
info: { readOnly: false, uuid: [Binary] },
idIndex: { v: 2, key: [Object], name: '_id_' }
},
{
name: 'maepr', // returns [] but it has data
type: 'collection',
options: {},
info: { readOnly: false, uuid: [Binary] },
idIndex: { v: 2, key: [Object], name: '_id_' }
},
{
name: 'txitem', // returns [] but it has data
type: 'collection',
options: {},
info: { readOnly: false, uuid: [Binary] },
idIndex: { v: 2, key: [Object], name: '_id_' }
},
{
name: 'empresas', // returns data
type: 'collection',
options: {},
info: { readOnly: false, uuid: [Binary] },
idIndex: { v: 2, key: [Object], name: '_id_', ns: 'suiteMBC.empresas' }
},
{
name: 'maeproxx', // returns [] but it has data
type: 'collection',
options: {},
info: { readOnly: false, uuid: [Binary] },
idIndex: { v: 2, key: [Object], name: '_id_' }
},
{
name: 'usuarios', // returns data
type: 'collection',
options: {},
info: { readOnly: false, uuid: [Binary] },
idIndex: { v: 2, key: [Object], name: '_id_', ns: 'suiteMBC.usuarios' }
}
]
【问题讨论】:
-
我怀疑这是模型名称和集合名称不同的常见问题(猫鼬会自动复数)。您能列出您尝试过的“收藏”名称吗?
maepr,例如,不是复数,所以你不能通过猫鼬模型maepr访问它,因为猫鼬可能会将这个maeprs作为集合名称。
标签: node.js mongodb mongoose backend