草庐IT

mongoose-plugins

全部标签

node.js - Mongoose 模式如何添加数组

我想知道我们如何在moongoose模式中添加一个字符串数组。我有以下代码,但它不起作用:varmessage=newSchema({topic:String,content:String,restriction:String,sender:String,reciever:String,users:[String],read:{type:String,default:'no'},like:{type:Number,default:0},created_at:{type:Date,default:Date.now}});我说的是用户。你能帮忙吗? 最佳答案

node.js - 使用 Mongoose 对数组字段进行排序

我正在尝试通过ts(时间戳)订购下一个模型:varSchema=newmongoose.Schema({info:{name:{type:String,trim:true,validate:[{validator:validations.user.maxName,msg:'Thenamemustbeshorter'}]}},gender:{type:String,trim:true,enum:['Male','Female']},notifications:[{story:{_id:{type:mongoose.Schema.Types.ObjectId,ref:'Story'},vi

javascript - MongoDB/Mongoose - 通过执行加法更新文档中的数字

我有一个mongoDB集合,用于存储节省的时间和成本。当用户第一次输入数据时文档看起来像这样:{"fullName":"johnsmith","company":"xyz","email":"john.smith@xyz.com","timeSaved":50//thisisinminutes"moneySaved":10//thisisindollars}现在,当用户再次输入数据时,我会发现该用户的文档是否已经存在,如果存在,我会得到它。这次用户输入:节省时间55省钱10我想将新值添加到现有值。所以在文件显示这个之后:"timeSaved":105//old50+new55"mone

node.js - 基于 Mongoose 中另外两个字段的第三个字段的计算

我在Mongoose模型上有2个字段:totalAttempts和totalRight。我打算根据它们计算准确度。totalAttempts:{type:Number,default:1},totalRight:{type:Number,default:1}这就是我所做的[它不起作用]accuracy:{type:Number,required:function(){return(this.totalRight/this.totalAttempts*100).toFixed(2)}}此外,如果我不为准确性设置默认值,我会收到错误消息:ERROR;Whilecreatingnewques

mongodb - 在运行查询之前未创建 Mongoose 索引

在运行我的测试时,我收到一条错误消息,指出我没有在标题字段上设置文本索引,但在运行我的应用程序时,文本搜索在同一模型下工作正常,不会抛出错误。textindexrequiredfor$textquery(nosuchcollection'test-db.torrents')importmongoosefrom'mongoose';importCategoryfrom'./category';constSchema=mongoose.Schema;constTorrent=newSchema({title:{type:String},category:{type:mongoose.Sch

idea编译maven项目报错:Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.3.1

Failedtoexecutegoalorg.apache.maven.plugins:maven-resources-plugin:3.3.1报错原因是maven版本过高导致。笔者idea2022.3版本,jdk1.8,maven用的3.9,在编译的时候,报上面这个错;项目中maven-plugin版本不匹配,可以调整自己的本地maven版本解决此问题。我是把自己的maven版本降低到3.8.1,编译通过。参考针对idea不同版本,使用不同的maven版本IDEA2022兼容Maven3.8.1及之前的所用版本​​​IDEA2021兼容Maven3.8.1及之前的所用版本​​IDEA2020

node.js - 如何使用 Mongoose 填充具有无限嵌套级别的文档

我正在设计一个网络应用程序来管理母公司和子公司的组织结构。有两种类型的公司:1-主公司,2-子公司。公司可以只属于一个公司,但可以有几个子公司。我的Mongoose架构如下所示:varcompanySchema=newmongoose.Schema({companyName:{type:String,required:true},estimatedAnnualEarnings:{type:Number,required:true},companyChildren:[{type:mongoose.Schema.Types.ObjectId,ref:'Company'}],companyT

node.js - 带有推送数组元素的 findOneAndUpdate 在 mongoose 中给出错误

我的查询如下图所示:constupdateLikes=(item_id,userInfo)=>{returnnewPromise((resolve,reject)=>{itemLike.findOneAndUpdate({'item_id':item_id},{$inc:{no_of_likes:1}},{"$push":{"users":userInfo}},{'new':true},(err,info)=>{if(err){reject(err);}else{if(info){resolve();}else{reject('noitemfound');}}});});};itemL

node.js - 在哪里建立 Mongoose 连接/任何数据库连接

关于mongoose.connect()和mongoose.createConnection()我已经阅读了几个stackoverflow文档和mongoose文档,但似乎没有对后者的明确解释,特别是在哪里建立连接。我不确定建立Mongoose或任何数据库连接的最佳位置在哪里,但我通常在我的server.js文件中进行。可以在模型本身中建立连接吗?使用mongoose.createConnection()时,似乎在模型中建立连接是唯一的选择,因为如果我在服务器文件中使用createConnection建立连接,我无法导出返回的连接对象并将其导入我的模型文件.当我只使用mongoose.

javascript - Mongoose find() 不适用于 $not 查询

这个问题在这里已经有了答案:mongodb$not_id(2个答案)关闭5年前。这是我的查找请求:Recipe.find({'author.id':{$not:{user}}}这是文档:https://docs.mongodb.com/manual/reference/operator/query/not/index.html这里是错误:Error:Can'tuse$notwithObjectId.我想找到所有不是由当前用户创作的食谱。这些论点工作正常,所以这不是问题。一定有办法做到这一点,还是我做错了什么?