Mongoose 只打开一些集合
Mongoose opens only some collections
我刚开始在我的项目中使用mongoose库,但出现了一个大问题。
我在数据库中有很多集合,当我在其中一些中使用 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 个。如果一个集合 returns 数据,它总是 returns 数据,其他的我删除,重新创建,什么都没有。
显而易见的问题:它们存在,它们有数据,它们是以相同的方式创建的。
用户可以read/write访问所有数据库
我也尝试过将模式与所有字段一起使用,答案相同(我想象的许多组合也可以工作)。
现在,当我使用 :
mongoose.connection.db.collection(collectionName,async (err,actualColection)=>{
const xx=await actualColeccion.find().toArray()
console.log(xx) // yeah !!! it returns data
})
它工作正常,它 returns 数据。
问题:
- 这是 mongo错误吗?
- 有没有更好的方法在 mongo 数据库中查找数据?
- 有没有比 mongoose 更好的包?
在此先感谢您的帮助。
pd: listCollections returns :
[
{
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' }
}
]
我怀疑这是模型名称和集合名称不同的常见问题(mongoose 自动复数)。你能列出你试过的'collection'个名字吗?例如,maepr 不是复数形式,因此您无法通过 mongoose 模型 maepr 访问它,因为 mongoose 可能会将此 maepr 作为集合名称。
尝试明确设置集合名称,例如:
modelo = new mongoose.model('maepr', new mongoose.Schema({}), 'maepr');
我刚开始在我的项目中使用mongoose库,但出现了一个大问题。 我在数据库中有很多集合,当我在其中一些中使用 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 个。如果一个集合 returns 数据,它总是 returns 数据,其他的我删除,重新创建,什么都没有。
显而易见的问题:它们存在,它们有数据,它们是以相同的方式创建的。
用户可以read/write访问所有数据库
我也尝试过将模式与所有字段一起使用,答案相同(我想象的许多组合也可以工作)。
现在,当我使用 :
mongoose.connection.db.collection(collectionName,async (err,actualColection)=>{
const xx=await actualColeccion.find().toArray()
console.log(xx) // yeah !!! it returns data
})
它工作正常,它 returns 数据。
问题:
- 这是 mongo错误吗?
- 有没有更好的方法在 mongo 数据库中查找数据?
- 有没有比 mongoose 更好的包?
在此先感谢您的帮助。
pd: listCollections returns :
[
{
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' }
}
]
我怀疑这是模型名称和集合名称不同的常见问题(mongoose 自动复数)。你能列出你试过的'collection'个名字吗?例如,maepr 不是复数形式,因此您无法通过 mongoose 模型 maepr 访问它,因为 mongoose 可能会将此 maepr 作为集合名称。
尝试明确设置集合名称,例如:
modelo = new mongoose.model('maepr', new mongoose.Schema({}), 'maepr');