我的应用程序被分成多个在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) 我对 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) 我的 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,但我不确定如何对这个对象数组执行此操作。
谢谢!
这是错误:
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) 您可以在他们的官方文档中的示例中看到它: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?
我在 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)
我能做什么?谢谢你。
我想念什么?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) 我试图完成一些非常简单的事情,但仍然失败了。
我想做的是当我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) 我有一个 mongoose 模式,用于存储端口号。我还为该字段设置了默认值。
port:{
type:Number,
default:1234
}
Run Code Online (Sandbox Code Playgroud)
如果我没有通过我的 API 获得任何值,它就会被设置为1234. 但是,如果有人发送null,它会接受null并保存到数据库。
不应该是隐蔽null的1234吗?null不是数字!我理解错了吗?
我正在考虑这里给出的解决方案,但我不想为没有它应该可以工作的东西添加额外的代码(除非我错了并且它不应该转换null为1234)
我有这个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)