标签: mongoose-schema

在多个微服务上使用猫鼬模式

我的应用程序被分成多个在heroku dynos上运行的微服务(它们无法访问彼此的文件)。有时,有多个微服务与一个集合一起使用。因此,这两个微服务都需要相应的猫鼬模式。

但是,并非两个微服务都需要完整架构。例如,微服务 A 需要完整架构,而微服务 B 仅需要该架构的几个字段。

微服务 A 内的示例架构:

var AccountSchema = mongoose.Schema({
    email: { type: String, required: true, unique: true },
    password: { type: String, required: true },
    phone: { type: String, required: true, unique: true },
    forename: { type: String, required: true },
    surname: { type: String, required: true },
    middleInitals: { type: String, required: false },
    failedLoginAttempts: { type: Number, required: true, default: 0 },
    lockUntil: { type: Number },
    createdAt: …
Run Code Online (Sandbox Code Playgroud)

mongoose mongodb node.js microservices mongoose-schema

5
推荐指数
1
解决办法
1826
查看次数

Mongoose 用户、角色和权限

我对 NoSQL/Mongo/Mongoose 很陌生,我正在尝试确定设置模式的最佳方法。这是我所拥有的:

const UserSchema = new mongoose.Schema(
  {
    email: {
      type: String,
      required: true
    },
    password: {
      type: String,
      required: true,
      minlength: 6,
      select: false,
    },
    roles: [
      {
        type: mongoose.Schema.ObjectId,
        ref: 'Role',
      },
    ],
  }
);
Run Code Online (Sandbox Code Playgroud)
const RoleSchema = new mongoose.Schema(
  {
    name: {
      type: String,
      required: true,
      unique: true,
    },
    description: {
      type: String,
    },
    permissions: [
      {
        type: mongoose.Schema.ObjectId,
        ref: 'Permission',
      },
    ],
  }
);
Run Code Online (Sandbox Code Playgroud)
const PermissionSchema = new mongoose.Schema(
  {
    name: {
      type: String,
      required: …
Run Code Online (Sandbox Code Playgroud)

mongoose mongodb node.js express mongoose-schema

5
推荐指数
1
解决办法
6512
查看次数

Mongoose 指定可选的对象数组

我的 mongo 收藏中的关键之一是

    options: [
      new mongoose.Schema(
        {
          answer: {
            type: String,
            required: true,
          },
          value: {
            type: Number,
            min: -10,
            max: 10,
            required: true,
          },
        },
        { _id: false }
      ),
    ],
Run Code Online (Sandbox Code Playgroud)

我在这里遇到的问题是,这options是可选的,但是当没有填写选项字段时,插入的文档有options: []

我相信我可以通过放置 a 来正常解决这个问题default: undefined,但我不确定如何对这个对象数组执行此操作。

谢谢!

mongoose mongodb mongoose-schema

5
推荐指数
1
解决办法
1984
查看次数

类型错误:无法读取未定义的属性“Symbol(mongoose#Document#scope)”

这是错误:

collection是保留的模式路径名,可能会破坏某些功能。您可以使用它,但使用风险由您自行承担。要禁用此警告,请传递supressReservedKeysWarning作为架构选项。/Users/sama/Desktop/spbackend/node_modules/mongoose/lib/helpers/document/compile.js:174 this.$set.call(this.$__[scopeSymbol] || this, path, v); ^

TypeError:无法读取 Model.set [作为集合] 处未定义的属性“Symbol(mongoose#Document#scope)”(/Users/sama/Desktop/spbackend/node_modules/mongoose/lib/helpers/document/compile.js:174 :32) 在 Function.compile (/Users/sama/Desktop/spbackend/node_modules/mongoose/lib/model.js:4801:30) 在 Mongoose._model (/Users/sama/Desktop/spbackend/node_modules/mongoose/lib /index.js:551:27) 在 Mongoose.model (/Users/sama/Desktop/spbackend/node_modules/mongoose/lib/index.js:509:27) 在对象。(/Users/sama/Desktop/spbackend/models/product.js:68:28) 在 Module._compile (internal/modules/cjs/loader.js:1063:30) 在 Object.Module._extensions..js (内部/modules/cjs/loader.js:1092:10) 在 Module.load (internal/modules/cjs/loader.js:928:32) 在 Function.Module._load (internal/modules/cjs/loader.js:769) :14) 在 Module.require (internal/modules/cjs/loader.js:952:19) [nodemon] 应用程序崩溃 - 启动前等待文件更改...

在产品型号中:

const mongoose = require('mongoose');

const productSchema = mongoose.Schema({
    name: {
        type: String,
        required: true
    },
    description: {
        type: String,
        required: true
    },
    richDescription: {
        type: String,
        default: ''
    },
    image: {
        type: …
Run Code Online (Sandbox Code Playgroud)

node.js typescript mongoose-schema

5
推荐指数
1
解决办法
3980
查看次数

猫鼬复合索引中的 1 和 -1 代表什么?

您可以在他们的官方文档中的示例中看到它:guide#indexes

var animalSchema = new Schema({
  name: String,
  type: String,
  tags: { type: [String], index: true } // field level
});

animalSchema.index({ name: 1, type: -1 }); // schema level
Run Code Online (Sandbox Code Playgroud)

为什么名称设置为1和类型设置为-1

mongoose mongodb mongoose-schema

4
推荐指数
1
解决办法
500
查看次数

如何使用带有动态键的猫鼬模型模式?

我在 nodejs 中使用猫鼬,我需要创建一个动态模式模型,这是我的代码:

schema.add({key : String});
Run Code Online (Sandbox Code Playgroud)

key = "user_name",但在我的数据库中,我发现模型将其作为键

{ key : "Michele" } and not { user_name: "Michele"}
Run Code Online (Sandbox Code Playgroud)

我能做什么?谢谢你。

node.js mongoose-schema

4
推荐指数
2
解决办法
1万
查看次数

如何为所有模式注册猫鼬插件?

我想念什么?Mongoose的文档说mongoose.plugin()为所有模式注册了一个插件。这是行不通的。我可以在每个架构上注册我的插件。

我的插件:

module.exports = function (schema, options) {

    schema.set('toObject',{
        transform: function (doc, ret, options) {
            return {
                test: 'It worked!'
            };
        }
    });
};
Run Code Online (Sandbox Code Playgroud)

我的架构:

var testPlugin = require('test-plugin.js');

var personSchema = mongoose.Schema({

    _id : { type: String, default: $.uuid.init },

    ssn : { type: String, required: true, trim: true },

    first : { type: String, required: true, trim: true },
    middle : { type: String, required: true, trim: true },
    last : { type: String, required: true, …
Run Code Online (Sandbox Code Playgroud)

mongoose mongodb node.js mongoose-schema

4
推荐指数
2
解决办法
1955
查看次数

使用猫鼬返回特定字段

我试图完成一些非常简单的事情,但仍然失败了。

我想做的是当我get在服务器上收到请求时,我想返回所有文档,但只返回填充的特定字段。

我的架构如下

var clientSchema = new Schema({
    name:{
        type: String,
        required: true
    },
    phone:{
        type: String,
        required: true
    },
    email:{
        type: String,
        required: true
    },
    address: {
        type: String,
        required: false
    }
});

var orderDetailsSchema = new Schema({
    //isn't added to frontend
   confirmed:{
       type: Boolean,
       required: true,
       default: false
   },    
   service:{
       type: String,
       required: true
   },
   delivery:{
       type: String,
       required: false
   },
    payment:{
        type: String,
        required: false
    },
    status:{
        type: String,
        required: true,
        default: "new order"
    }, …
Run Code Online (Sandbox Code Playgroud)

javascript mongoose mongodb node.js mongoose-schema

4
推荐指数
1
解决办法
9571
查看次数

Mongoose 接受数字字段的空值

我有一个 mongoose 模式,用于存储端口号。我还为该字段设置了默认值。

port:{
    type:Number,
    default:1234
}
Run Code Online (Sandbox Code Playgroud)

如果我没有通过我的 API 获得任何值,它就会被设置为1234. 但是,如果有人发送null,它会接受null并保存到数据库。

不应该是隐蔽null1234吗?null不是数字!我理解错了吗?

我正在考虑这里给出的解决方案,但我不想为没有它应该可以工作的东西添加额外的代码(除非我错了并且它不应该转换null1234

mongoose node.js mongoose-schema

4
推荐指数
1
解决办法
5924
查看次数

如何使用对象数组创建猫鼬模式

我有这个json:

{
    "data": [
        "id": "1",
        "name": "Sample test",
        "description": "this is a sample test",
        "category": "tests",
        "points": 100,
        "startDate"?:?"2018-02-15 00:00:00"?,
        "endDate"?:?"2018-02-22 00:00:00"?,
        "isActive"?:?true?,
        "alreadyAnswered"?:?false?,
        "questions"?:[
            {
                "id": 1,
                "text": "What is your name",
                "type": "text",
            },
            {
                "id": 2,
                "text": "What is your favorite color",
                "type": "select",
                "options": [
                    {
                        "id": 1,
                        "text": "Red",
                        "value": "red"
                    },
                    {
                        "id": 2,
                        "text": "Blue",
                        "value": "blue"
                    }
                ]
            }
        ]
    ]
}
Run Code Online (Sandbox Code Playgroud)

我需要将此json创建到mongo数据库中,以便可以通过节点应用程序获取它。

这是我当前的模式:

let TestSchema = new Schema({
    id: Number, …
Run Code Online (Sandbox Code Playgroud)

mongodb node.js mongoose-schema

4
推荐指数
1
解决办法
5767
查看次数