草庐IT

mongoose-encryption

全部标签

node.js - Mongoose - 检查 `upsert` 是否创建了新文档

有什么方法可以知道Mongoose的upsert选项是否通过await创建了一个新文档?本质上我想做this除了没有回调。注意:我正在使用findByIdAndUpdate 最佳答案 mongoose函数的callback、asyncawait甚至with.then都没有区别回调User.update({_id:usr._id},upsertData,{upsert:true},function(err,num){console.log(err)console.log(num)}异步等待try{constnum=awaitUser.

node.js - Mongoose 更新或插入许多文件

我正在尝试使用最新版本的mongoose插入对象数组,或者在相应的产品ID已存在时进行更新。我终其一生都无法找出正确的使用方法(bulkWrite、updateMany等),而且我似乎无法在不出错的情况下找出语法。例如,我正在尝试Product.update({},products,{upsert:true,multi:true},(err,docs)=>console.log(docs))这会引发错误DeprecationWarning:collection.updateisdeprecated.UseupdateOne,updateMany,orbulkWriteinstead.M

javascript - 在一次调用中推送 mongoose 中的数组列表

我使用promise和Mongoose,我想将数组插入数组。数组架构和函数:Temp:[{code:{type:Number,required:true}}],vararr=[2525,2525,2525,2525]Room.where({_id:newObjectID(_RoomID)}).updateOne({$addToSet:{Temp:{code:arr}}}).then(()=>{},()=>{})我使用了这段代码,但没有使用arr,如果我设置一个单一的var数字ex:变量arr=2525一切都正确并存储我怎样才能用数组列表做到这一点???更新:我的问题已经解决了。我使用了

mongodb - Mongoose:尝试使用 .virtual 方法重命名

我必须在使用填充时重命名字段的名称。constCategorySchema=newSchema({name:{type:String,unique:true},featured:{type:Boolean,default:true},image:String,active:{type:Boolean,default:true},subCategoryIds:[{type:Schema.Types.ObjectId,ref:'SubCategory'}]},{timestamps:true});exportdefaultmongoose.model('Category',Category

node.js - Mongoose 填充排序_Mongoose Node.js

我有两个模式代表我的帖子和类别文档。我正在尝试使用category.order属性对我的帖子进行排序。order属性是一个数字字段。constpostSchema=mongoose.Schema({title:{type:String,max:200,},body:{type:String,max:10000,},categories:{type:mongoose.Schema.ObjectId,ref:'Category',required:true}})module.exports=mongoose.model('Post',postSchema);constcategorySch

node.js - Mongoose promise 和 sonarqube

我喜欢asyncawait语法,我在mongoose中经常使用它。所以在我的项目中有很多:constuser=awaitUser.findOne({_id:req.params.id})正如预期的那样工作。但是,在sonarqube中,我有这些错误:在非promise上重构这个冗余的“等待”。Sonar规则i:ItispossibletouseawaitonvalueswhicharenotPromises,butit'suselessandmisleading.ThepointofawaitistopauseexecutionuntilthePromise'sasynchronous

node.js - 如何比较 MongoDB(NodeJS + Mongoose )中的日期?

我在MongoDB中有一些数据,我想比较存储在其中的两个日期之间的日期。问题是当我在查询中使用$gte或$lte时它总是返回null。架构:constSettingSchema=newSchema({advertisment:[{name:{type:String,required:true},fromdate:{type:Date,default:Date.now()},todate:{type:Date},active:{type:Boolean}}]});示例数据:{"advertisment":[{"fromdate":"2010-06-29T06:53:32.643Z","_

javascript - 如何使用 Mongoose 从 mongoDB 中获取一定数量的对象? ( Node .js)

我有一个MongoDB数据库和一个React网页,该网页使用对我的Node.js服务器的http请求来显示内容。此Node.js使用mongoose与数据库通信。React网页每页最多显示10个条目,当数据库变大时速度很慢:ReactwebpageNode.js正在做什么:router.get("/",async(req,res)=>{constmovies=awaitMovie.find().select("-__v").sort("name");res.send(movies);});如您所见,Node.js返回所有数据库,前端React仅显示10个条目,具体取决于用户正在查看的页

mongodb - Mongoose 更新数组中对象的单个值

我目前正在尝试通过使用以下数据将单个用户的数组(电影列表)中ID“12345”的对象的值(状态)从“计划观看”更新为“观看”{_id:"5d4813c0a14fcd44f83feda8",userName:"Dave",movielist:[{_id:"5d29c4922ce984356cea8b48",movie:{_id:"12345"title:"TheAvengers",movieLength:"143min",}status:"Plantowatch"},{_id:"5d276dd65f27682c26c6041b",movie:{_id:"5d28ca94e2e19d6cb

javascript - 使用 Graphql 时在 Mongoose 模式中填充 "Ref"

我正在使用Graphql,然后我遇到了需要填充的情况,但我不知道如何执行它。这是我的预订模式constmongoose=require('mongoose')constSchema=mongoose.Schemaconstbookingschema=newSchema({event:{type:Schema.Types.ObjectId,ref:'Event'},user:{type:Schema.Types.ObjectId,ref:'User'}},{timestamps:true})module.exports=mongoose.model('Booking',bookingsc