如何在不定义模式的情况下使用Mongoose?

wljmcqd8  于 12个月前  发布在  Go
关注(0)|答案(7)|浏览(122)

在以前版本的Mongoose(用于node.js)中,有一个选项可以在不定义模式的情况下使用它

var collection = mongoose.noSchema(db, "User");

但在当前版本中,“noSchema”函数已被删除。我的模式可能会经常改变,而且确实不适合定义的模式,那么有没有一种新的方法可以在mongoose中使用无模式模型?

lhcgjxsq

lhcgjxsq1#

我想这就是你要找的 Mongoose 严格

选项:严格

strict选项(默认情况下启用)确保添加到模型示例中的、在模式中未指定的值不会保存到数据库中。

注意:除非有充分的理由,否则不要设置为false。

var thingSchema = new Schema({..}, { strict: false });
    var Thing = mongoose.model('Thing', thingSchema);
    var thing = new Thing({ iAmNotInTheSchema: true });
    thing.save() // iAmNotInTheSchema is now saved to the db!!
5m1hhzi4

5m1hhzi42#

实际上,“混合”(Schema.Types.Mixed)模式似乎在Mongoose中做到了这一点。
它接受一个无模式的自由形式的JS对象-所以你可以扔给它任何东西。看起来你必须在之后手动触发保存该对象,但这似乎是一个公平的权衡。

混合

一个“任何事情都可以”的SchemaType,它的灵活性来自于它更难维护的代价。Mixed可以通过Schema.Types.Mixed或传递一个空的对象字面量来使用。以下是等效的:

var Any = new Schema({ any: {} });
var Any = new Schema({ any: Schema.Types.Mixed });

因为它是一个无模式类型,你可以将值更改为任何你喜欢的值,但是Mongoose失去了自动检测和保存这些更改的能力。要“告诉”Mongoose Mixed类型的值已经更改,请调用文档的.markModified(path)方法,将路径传递到您刚刚更改的Mixed类型。

person.anything = { x: [3, 4, { y: "changed" }] };
person.markModified('anything');
person.save(); // anything will now get saved
  • Mongoose模式类型
v2g6jxz6

v2g6jxz63#

克里斯,你看一下Mongous。我在mongoose上也遇到了同样的问题,因为我的模式在开发中变化非常频繁。Mongous允许我拥有Mongoose的简单性,同时能够松散地定义和更改我的“模式”。我选择简单地构建标准JavaScript对象并将它们存储在数据库中,如下所示

function User(user){
  this.name = user.name
, this.age = user.age
}

app.post('save/user', function(req,res,next){
  var u = new User(req.body)
  db('mydb.users').save(u)
  res.send(200)
  // that's it! You've saved a user
});

比Mongoose简单得多,尽管我相信你错过了一些很酷的中间件,比如“pre”。但我不需要这些。希望这对你有帮助!!!

hc8w905p

hc8w905p4#

以下是详细描述:[https://www.meanstack.site/2020/01/save-data-to-mongodb-without-defining.html][1]

const express = require('express')()
    const mongoose = require('mongoose')
    const bodyParser = require('body-parser')
    const Schema = mongoose.Schema

    express.post('/', async (req, res) => {
        // strict false will allow you to save document which is coming from the req.body
        const testCollectionSchema = new Schema({}, { strict: false })
        const TestCollection = mongoose.model('test_collection', testCollectionSchema)
        let body = req.body
        const testCollectionData = new TestCollection(body)
        await testCollectionData.save()
        return res.send({
            "msg": "Data Saved Successfully"
        })
    })

  [1]: https://www.meanstack.site/2020/01/save-data-to-mongodb-without-defining.html
z9gpfhce

z9gpfhce5#

注意:{ strict: false }参数将适用于创建和更新。

hlswsv35

hlswsv356#

方案模型中增加{strict: false },保存事件中增加{validateBeforeSave: false }

let MyScheme = new Schema(
    { <optional - basic fields if you want> ...},
    {strict: false }
)
let doc = new MyScheme(req.body)
doc.save({validateBeforeSave: false });
s1ag04yj

s1ag04yj7#

这是不可能了。
您可以将Mongoose与具有模式和节点驱动程序的集合一起使用,或者使用其他mongo模块来处理那些无模式的集合。
https://groups.google.com/forum/#!msg/mongoose-orm/Bj9KTjI0NAQ/qSojYmoDwDYJ

相关问题