猫鼬指定对象的可选数组

时间:2020-08-19 20:32:58

标签: mongodb mongoose mongoose-schema

我的mongo集合中的关键之一是

import numpy as np
import gym
from collections import deque
    from keras.layers import Input, Dense
    from keras.models import Sequential
    from keras.optimizers import Adam
    from keras.models import Model, load_model
    import numpy as np
    import random
    
    def DeepNNModel():
        model  = Sequential()
        model.add(Dense(64, input_dim = 4, activation="relu"))
        model.add(Dense(64, activation="relu"))
        model.add(Dense(2))
        model.compile(loss = "mean_squared_error", optimizer = Adam(lr=1e-3))
        print(model.summary())
        return model
    
    env = gym.make('CartPole-v1')
    model = DeepNNModel()
    state = env.reset()
    state = np.reshape(state, [1, 4])
    
    action = np.zeros([2])
    action[0] = 1
    action = np.reshape(action, [1, 2])
    print ("state: ", state)
    print ("action: ", action)
    
    discounted_r = arr = np.array([ [1] ])
    print ("discounted_r.shape: ", discounted_r)
    print ("action.shape: ", action.shape)
    
    #model.fit(state, action, epochs=1, verbose=0)
    model.fit(state, action, sample_weight=discounted_r, batch_size=1, epochs=1, verbose=0)

我在这里遇到的问题是 options: [ new mongoose.Schema( { answer: { type: String, required: true, }, value: { type: Number, min: -10, max: 10, required: true, }, }, { _id: false } ), ], 是可选的,但是当没有选项字段填写时,插入的文档中就有options

我相信我可以通过放置一个options: []来解决此问题,但是我不确定如何针对此对象数组进行此操作。

谢谢!

1 个答案:

答案 0 :(得分:0)

在猫鼬中,空数组是数组类型的默认值。您可以通过以下方式使用default字段来覆盖它:

let optionsSchema = new mongoose.Schema(
    {
        answer: {
            type: String,
            required: true,
        },
        value: {
            type: Number,
            min: -10,
            max: 10,
            required: true,
        },
    },
    { _id: false });


const RootSchema = new Schema({
    options : {
        type: [optionsSchema],
        default: undefined
    }
})