我有以下架构。
var UserSchema = new mongoose.Schema({
username: {
type: String,
unique: true,
required: true
},
password: {
type: String,
required: true
},
test: {
type: String,
default: 'hello world'
}
});
UserSchema.pre('save', function(callback) {
var user = this;
this.test = undefined; // here unset test field which prevent to add in db
});
module.exports = mongoose.model('User', UserSchema);
Run Code Online (Sandbox Code Playgroud)
但是当我找到数据时
User.find(function(err, users) {
if (err)
res.send(err);
res.json(users);
});
Run Code Online (Sandbox Code Playgroud)
它总是返回
[
{
_id: "56fa6c15a9383e7c0f2e4477",
username: "abca",
password: "$2a$05$GkssfHjoZnX8na/QMe79LOwutun1bv2o76gTQsIThnjOTW.sobD/2",
__v: 0,
test: "hello world" …Run Code Online (Sandbox Code Playgroud) 我正在使用 MEAN 堆栈并尝试使用 Mongoose 将数据保存到我的 MongoDB 数据库中。但是,我的模型不断收到错误“电影不是构造函数”。但这就是猫鼬文档的显示方式,所以我不确定我做错了什么..
请帮忙!
电影.js
let mongoose = require('mongoose');
let MovieSchema = new mongoose.Schema({
title: String,
genre: String
});
export let Movie = mongoose.model("Movie", MovieSchema);
Run Code Online (Sandbox Code Playgroud)
路由.js
import express = require('express');
import mongoose = require('mongoose');
let Movie = require('../models/Movie');
let router = express.Router();
router.post('/movies', function(req, res, next) {
let new_movie = new Movie(req.body);
new_movie.save(function(err, movie) {
if(err) return next(err);
res.send(movie);
});
});
Run Code Online (Sandbox Code Playgroud) 定义Mongoose模式时,通常应谨慎指定应存在哪些索引.也就是说,在许多情况下,我们希望控制创建的索引的名称,尤其是当这些索引是复合的,这样它们是可以理解的.
实际上,在创建某些索引时,需要明确指定索引名称以避免超出index name length limit.
由于ensureIndex(在默认情况下)对模式中定义的索引进行调用,因此控制由ensureIndex创建的索引名称的适当语法是什么?我认为使用字段级索引语法是不可能的,但它肯定可用于模式级索引吗?
var ExampleSchema = new Schema({
a: String,
b: String,
c: Date,
d: Number,
e: { type: [String], index: true } // Field level index
});
// We define compound indexes in the schema
ExampleSchema.index({a: 1, b: 1, c: 1});
ExampleSchema.index({d:1, e:1}, {unique: true});
Run Code Online (Sandbox Code Playgroud)
值得注意的是,db.collection.ensureIndex已弃用(通过mongodb),现在是别名db.collection.createIndex.
我有这样的猫鼬模式
var mongoose = require ('mongoose');
var bcrypt = require('bcryptjs');
var Schema = mongoose.Schema;
var SALT_WORK_FACTOR = 10;
var touristSchema = new Schema ({
local: {
email: String,
password: String
},
facebook: {
id: String,
token: String,
email: String,
name: String,
}
});
touristSchema.pre('save', function(next) {
var user = this;
console.log('bcrypt called by strategy', user);
// if user is facebook user skip the pasword thing.
if (user.facebook.token) {
next();
}
// only hash the password if it has been modified …Run Code Online (Sandbox Code Playgroud) 我有一个同时设置时间戳和鉴别器键选项的模式。使用Model.save(),一切正常,但Model.bulkWrite既不使用我的鉴别键也不使用时间戳被保存/更新。
mongoose 有没有办法批量更新文档(每个文档都有不同的值)并维护关于模式选项的行为,比如 in Model.save()?
我的用例是我有大约 500 个文档,我必须检查每个文档是否已经存在(如果存在则更新它的一部分),如果不存在则插入一个完整的新记录。除了模式选项不起作用之外,这适用于bulkWrite一系列updateOne操作。
我想在我的猫鼬模型中有一个对象(例如“成分”),其中键是 ObjectID,它们的值是数字。有可能这样做吗?我应该如何定义我的猫鼬模式?您可以在下面找到一个示例。
示例 JSON:
{
"_id": ""5a2539b41c574006c46f1a07",
"name": "xyz",
"ingredients": {
"5a23f5e6159f5c3438c75971": 50,
"5a23f60b159f5c3438c75972": 50,
"5a255b04c9d9c40ac8927dd5": 50
}
}
Run Code Online (Sandbox Code Playgroud)
提前谢谢你的帮助。
使用 Mongoose ORM 进行 MongoDB
我声明了一个猫鼬静态方法,如下所示:
ConvoDataSchema.statics.randomItem = async function () { ... }
Run Code Online (Sandbox Code Playgroud)
然后用它创建一个模型
const ConvoData = mongoose.model('ConvoData', ConvoDataSchema)
Run Code Online (Sandbox Code Playgroud)
但后来当我想调用该方法时:
let convoData = await ConvoData.randomItem()
Run Code Online (Sandbox Code Playgroud)
我的 linter 不知道ConvoDataMongoose 已经修补了这个神奇的方法。
我如何声明这些方法,以便 Linter (TSLint / VSCode Intellisense) 可以正确发现这些方法?
使用 mongoose-uuid 库时,我可以为模式设置 UUID 类型,因此当我读取数据时,它采用字符串 (utf-8) 格式,而当我保存数据时,它采用 UUID ObjectID BSON 类型 4 格式。这对于我的架构中的顶级或平面直接值和引用定义非常有效。但是,当我在架构中的引用数组中有 UUID 时,该数组会正确保存到数据库,但是当它呈现时,它是原始类型。根据下面的示例,您可以看到scope_id 以正确的格式显示,但权利却不然。
以下是我正在使用的版本: mongoose-uuid - 2.3.0 mongoose - 5.5.11
我尝试通过更改 getter 和转换值来修改库(mongoose-uuid),但是,当我这样做时,它在呈现时有效,但在保存到数据库时失败。这很可能是由于该值在保存到数据库之前进行了转换或转换。
这是一个示例架构
{
"code": {
"type": String,
"required": true
},
"scope_id": {
"type": mongoose.Types.UUID,
"ref": "scopes"
},
"entitlements": [{
"type": mongoose.Types.UUID,
"ref": "entitlements"
}]
}
Run Code Online (Sandbox Code Playgroud)
实际响应示例
{
"entitlements": [
"zMihi1BKRomM1Q41p7hgLA==",
"ztOYL7n1RoGA6aoc0TcqoQ=="
],
"code": "APPUSR",
"scope_id": "b8f80c82-8325-4ffd-bfd7-e373a90e7c45",
"id": "32e79061-e531-45ad-b934-56811e2ad713"
}
Run Code Online (Sandbox Code Playgroud)
预期反应
{
"entitlements": [
"ccc8a18b-504a-4689-8cd5-0e35a7b8602c",
"ced3982f-b9f5-4681-80e9-aa1cd1372aa1"
],
"code": "APPUSR",
"scope_id": "b8f80c82-8325-4ffd-bfd7-e373a90e7c45",
"id": "32e79061-e531-45ad-b934-56811e2ad713"
}
Run Code Online (Sandbox Code Playgroud) 我的应用程序被分成多个在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) mongoose-schema ×10
mongoose ×9
node.js ×8
mongodb ×7
express ×3
eslint ×1
passport.js ×1
uuid ×1