在Mongoose(用于node.js)的先前版本中,有一个选项可以在不定义模式的情况下使用它。
var collection = mongoose.noSchema(db, "User");
但是在当前版本中,“noSchema”函数已被删除。我的模式可能经常更改,并且真的不适合定义好的模式,那么在mongoose中是否有一种新的方法可以使用无模式模型?
在Mongoose(用于node.js)的先前版本中,有一个选项可以在不定义模式的情况下使用它。
var collection = mongoose.noSchema(db, "User");
但是在当前版本中,“noSchema”函数已被删除。我的模式可能经常更改,并且真的不适合定义好的模式,那么在mongoose中是否有一种新的方法可以使用无模式模型?
我认为这就是你正在寻找的 Mongoose Strict
选项:严格模式
默认情况下启用的 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!!
thing.set(key, value)
而不是thing.key=value
。 - laggingreflexMySQL
的说法 :) 我认为Jonathan的建议/根据Mongoose API文档的说明__注意:除非你有充分的理由,否则不要将其设置为false__。在当前上下文中(仅关于NO-SQL
)是完全可以的。 - Amol M Kulkarni实际上在Mongoose中,“Mixed” (Schema.Types.Mixed
) 模式似乎正好能做到这一点...
它接受一个无需模式定义、自由形式的JS对象 - 因此你可以随便放什么在里面。不过似乎需要手动触发该对象的保存,但这似乎是一个公平的权衡。
Mixed
"任何事物都可以"的SchemaType,其灵活性使得它更难以维护。Mixed可通过
Schema.Types.Mixed
或通过传递空的对象字面量来使用。以下两种方式是等效的:
var Any = new Schema({ any: {} }); var Any = new Schema({ any: Schema.Types.Mixed });
由于混合类型是无模式的,因此您可以将值更改为任何其他值,但Mongoose会失去自动检测和保存这些更改的能力。要“告诉”Mongoose混合类型的值已更改,请调用文档的
.markModified(path)
方法,并将刚刚更改的混合类型的路径传递给它。person.anything = { x: [3, 4, { y: "changed" }] }; person.markModified('anything'); person.save(); // anything will now get saved
嗨Chris,看看Mongous。我在开发中遇到了和mongoose一样的问题,因为我的Schemas现在非常频繁地变化。Mongous让我保持了Mongoose的简单性,同时能够宽松定义和更改我的'schemas'。我选择创建标准的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"。但是我并不需要这些。希望这可以帮到你!
以下是详细说明: [https://www.meanstack.site/2020/01/save-data-to-mongodb-without-defining.html][1]
在不定义模式的情况下将数据保存到MongoDB 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
mongoose.connection.collection
。import mongoose from 'mongoose';
const collection = mongoose.connection.collection('user')
// use it like a mongodb collection
await collection.updateOne({ name: "name" }, { $set: { name: "new_name" } })
{ strict: false }
适用于创建和更新操作。{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 });
这已经不再可能了。
您可以使用具有模式的集合和节点驱动程序或其他Mongo模块来使用Mongoose处理这些无模式的集合。
https://groups.google.com/forum/#!msg/mongoose-orm/Bj9KTjI0NAQ/qSojYmoDwDYJ