gpt4 book ai didi

node.js - Mongoose:从现有数据生成模式

转载 作者:IT老高 更新时间:2023-10-28 13:34:37 24 4
gpt4 key购买 nike

例如,我在 db 中有 user 集合:

{'_id':0, 'name': 'Joe', 'score':[80, 33]}
{'_id':1, 'name': 'Moe', 'score':[90, 81]}
... ...

如何以现有格式读取这些数据,这意味着,使用它的现有架构无需创建新架构

我阅读了 Mongoose 文档并搜索了一段时间,但没有找到满意的答案。

最佳答案

将 mongoose 与现有集合一起使用时,您必须在 mongoose 架构中引用该集合。这样做的方法是将集合的名称添加到您的架构中。因此,如果您的集合位于 'mongodb://localhost:27017/test' 并称为 'things',您将使用:

~~~
const Schema = mongoose.Schema;
const ThingSchema = new Schema({
name: {
type: String
}
});

const Model = mongoose.model('Thing', ThingSchema, 'things');
module.exports = Model;
~~~

感谢 http://chrisflx.blogspot.fr/2014/04/nodejs-mongoose-with-preexisting-data.html?m=1

关于node.js - Mongoose:从现有数据生成模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24314393/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com