【问题标题】:Mongoose opens only some collectionsMongoose 只开放部分收藏
【发布时间】: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
})

它工作正常,它返回数据。

问题:

  1. 这是猫鼬错误吗?
  2. 有没有更好的方法在 mongo 数据库中查找数据?
  3. 有比猫鼬更好的包吗?

提前感谢您的帮助。

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


【解决方案1】:

我怀疑这是模型名称和集合名称不同的常见问题(猫鼬会自动复数)。您能列出您尝试过的“收藏”名称吗?例如,maepr 不是复数,因此您无法通过 mongoose 模型 maepr 访问它,因为 mongoose 可能会将这个 maepr 作为集合名称。

尝试明确设置集合名称,例如:

modelo =  new mongoose.model('maepr', new mongoose.Schema({}), 'maepr');

【讨论】:

  • 这正是发生的事情,朋友,(我试图弄清楚 3 天)非常感谢。
  • 这很混乱!我希望他们不要试图变得如此“聪明”。
  • 是的,其实s不代表复数,有很多词尾没有s,都是复数(people,mice等,也就是英文,我西班牙语代码)。有史以来最糟糕的主意......
  • 同意!看看他们拥有的这些“多元化”规则。坚果! github.com/Automattic/mongoose/blob/… -- 外语是对这种疯狂想法的完美反驳。
猜你喜欢
  • 2018-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-26
  • 1970-01-01
  • 1970-01-01
  • 2018-02-01
  • 1970-01-01
相关资源
最近更新 更多