我想使用mongoose自定义验证来验证endDate是否大于startDate.如何访问startDate值?使用this.startDate时,它不起作用; 我得到了不确定.
var a = new Schema({
startDate: Date,
endDate: Date
});
var A = mongoose.model('A', a);
A.schema.path('endDate').validate(function (value) {
return diff(this.startDate, value) >= 0;
}, 'End Date must be greater than Start Date');
Run Code Online (Sandbox Code Playgroud)
diff 是一个比较两个日期的函数.
我想使用findAndModify使用Mongoose以原子方式递增字段.
但是,下面的代码会引发错误"TypeError:Object#have method'seadAndModify'":
// defining schema for the "counters" table
var tableSchema = new Schema({
_id: String,
next: Number
});
// creating table object for the counters table
var counters_table = mongoose.model('counters', tableSchema);
var tableObj = new counters_table();
// increment the "next" field on the table object
var query = {_id: 'messagetransaction'};
var update = {'$inc': {next: 1}};
var ret = tableObj.findAndModify(query, [], update, true, true, function(err) {
if (err) {
throw err;
}
else {
console.log("updated!");
}
});
Run Code Online (Sandbox Code Playgroud) 我在另一个答案中看到,为了包含虚拟字段,您必须执行https://groups.google.com/forum/?fromgroups#!topic/mongoose-orm/HjrPAP_WXYs
var schemaOptions = {
toJSON: {
virtuals: true
}
};
Run Code Online (Sandbox Code Playgroud)
我做过的;
现在在架构中:
new Schema({...}, schemaOptions);
Run Code Online (Sandbox Code Playgroud)
但仍然如此,数据不包括虚拟..:s
但是像这样工作:
var docsCallback = function(err, docs){
var i = docs.length;
var nDocs = [];
while(i--){
nDocs[i] = docs[i].toObject({virtuals: true});
}
done(nDocs);
}
Run Code Online (Sandbox Code Playgroud) 我是mongodb的超级新手.我正在使用mongoose从node.js访问mongodb,并且知道如何让事情发挥作用,但我不认为我理解为什么它会像它一样工作.
最重要的是,当mongodb的一个突出特点是它没有架构时,我不明白为什么mongoose有'架构'.有人可以开导我吗?谢谢.
我试图在mongoose中使用'findOneAndUpdate',我发送的更新的JS对象没有保存到mongo.保存时我没有收到错误,但是我确实为更新的对象取回了null.我有什么想法可能做错了吗?这个示例是尝试更新存储在mongo中的整个对象,即覆盖名称对象.
var query = {"_id": id};
var update = {name: {first: 'john', last: 'smith'}};
var options = {new: true};
People.findOneAndUpdate(query, update, options, function(err, person) {
if (err) {
console.log('got an error');
}
// at this point person is null.
});
Run Code Online (Sandbox Code Playgroud) 我正在尝试通过计算db中的文档来动态创建我的Mongoose模型的_id,并使用该数字创建_id(假设第一个_id为0).但是,我无法从我的值中设置_id.这是我的代码:
//Schemas
var Post = new mongoose.Schema({
//_id: Number,
title: String,
content: String,
tags: [ String ]
});
var count = 16;
//Models
var PostModel = mongoose.model( 'Post', Post );
app.post( '/', function( request, response ) {
var post = new PostModel({
_id: count,
title: request.body.title,
content: request.body.content,
tags: request.body.tags
});
post.save( function( err ) {
if( !err ) {
return console.log( 'Post saved');
} else {
console.log( err );
}
});
count++;
return response.send(post);
});
Run Code Online (Sandbox Code Playgroud)
我试图将_id设置为多种不同的方式,但它对我不起作用.这是最新的错误:
{ message: 'Cast …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用GUI更新集合(单元)中的文档,并在更新后,我想更新集合(用户)中的值(user.Units,它是一个单元名称数组).如果数组长度只是1个元素,它会更新并显示在数据库中,一切运行良好,但是当单元数组有多个元素时,我尝试通过for循环更新它,它显示它会更新但是当我检查数据库它仍然没有更新.
当我通过循环更新值时,我真的无法弄清楚为什么它不更新数据库.
整个编辑和更新功能: -
edit_unit: function (req, res, next) {
var Data = req.body;
Client_data.Unit.findById(req.params.unitId, function (err, unit) {
var error = false;
if (err) {
error = err;
} else if (!unit) {
error = "FATAL: unable to look up Unit #" + req.params.unitId;
} else {
switch(req.body.name) {
case 'Icon':
var Icon = unit.Icon;
User.find({"Units":Icon}, function (err, users) {
if (err)
console.log(err);
users.forEach(function (u) {
if (u.Units.length > 1) {
for (var i = 0; i <= u.Units.length; …Run Code Online (Sandbox Code Playgroud) 我有一个用 typescript 编写的 Express 服务器,并"module": "es2020"在其 tsconfig.xml 中。
我还es2020为我的 graphql API 开发了另一个模块,仍然是 typescript,这个模块使用 mongoose 和这样的命名导入:
import { Types } from 'mongoose'
Run Code Online (Sandbox Code Playgroud)
当我用 编译我的 graphql 模块时,一切正常tsc。但是运行的快递服务器
nodemon --watch './**/*.ts' --exec 'node --experimental-specifier-resolution=node --loader ts-node/esm' src/index.ts
无法处理名为 import 的猫鼬。
import { Types } from 'mongoose';
^^^^^
SyntaxError: Named export 'Types' not found. The requested module 'mongoose' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via …Run Code Online (Sandbox Code Playgroud) 对于我的项目,我想为组织组保留一份mongoose文档,如下所示:
var groupSchema = Schema({
name : { type : String },
org : { type : Schema.Types.ObjectId, ref : 'Organization' },
...
users : [{
uid : { type : Schema.Types.ObjectId, ref : 'User' },
...
}]
});
Run Code Online (Sandbox Code Playgroud)
我想阻止同一个用户在同一组中两次.为此,我需要强制users.uid在users数组中是唯一的.我尝试为uid说明'unique:true',但这不起作用.有没有办法用mongoose或mongoDB执行此操作而无需额外查询或拆分架构?
编辑:我将uid的先前值更改为uid:{type:Schema.Types.ObjectId,ref:'User',index:{unique:true,dropDups:true}}但是这似乎仍然不起作用.
编辑:假设没有简单的方法来实现这一点,我添加了一个额外的查询检查用户是否已经在组中.在我看来,这是最简单的方法.