草庐IT

sinon-mongoose

全部标签

node.js - 如何访问预更新 Mongoose 中间件中的文档对象?

考虑一下,我有一个像这样的Mongoose模式constmySchema=newmongoose.schema({val1:Number,val2:Numberval_1_isBigger:Boolean})现在我想在每次更新操作之前比较val1和val2,因此我想为val_1_isBigger设置bool属性。我的问题是如何在pre('update')mongoose插件操作期间访问文档对象。看下面的例子mySChema.plugin(function(schema,options){schema.pre('update',function(next){//HereHowDoIcom

node.js - Mongo/Mongoose 聚合 - $redact 和 $cond 问题

我很幸运能够得到另一个SO问题的精彩答案Mongo/Mongoose-AggregatingbyDate来自@chridam,它给出了一组文档,例如:{"_id":ObjectId("5907a5850b459d4fdcdf49ac"),"amount":-33.3,"name":"RINGGO","method":"VIS","date":ISODate("2017-04-26T23:00:00Z"),"importDate":ISODate("2017-05-01T21:15:49.581Z"),"category":"NotSet","__v":0}{"_id":ObjectId

javascript - Mongoose 切换多个连接

我正在使用Node.Js8.6和Mongoose4.11,并且有多个数据库连接。数据库连接是通过mongoose.createConnection建立的。我发现mongoose对象具有connections属性(数组),我可以在其中看到已建立的连接。我的问题是,在单独的模块中创建数据库模型时,在连接之间切换的正确方法是什么。索引.jsasyncfunctiondb1(){awaitmongoose.createConnection(process.env.MONGODB_URI_1,{useMongoClient:true});}asyncfunctiondb2(){awaitmong

mongodb - 在 Mongo 中使用 Cursor() 与 Mongoose 聚合

我最近在我的开发环境中更新了mongo以利用新功能(Mongod1.4-->4.0)。现在我所有现有的聚合查询都返回错误:{[MongoError:The'cursor'optionisrequired,exceptforaggregatewiththeexplainargument]name:'MongoError',ok:0,errmsg:'The\'cursor\'optionisrequired,exceptforaggregatewiththeexplainargument',code:9,codeName:'FailedToParse'}我知道我应该能够将“cursor()

node.js - Mongoose 条件必填字段验证

根据Mongoosebuilt-invalidatorsdocumentations,我可以使用条件必填字段:constschema=mongoose.Schema({a:{type:String,required:function(){returnthis.b===1}},b:{type:Number,required:true}});在此架构中,仅当属性b等于1时才需要属性a。尝试创建一个新文档按预期工作:Model.create({b:1});//throwsValidationError(propertyaisrequired)和Model.create({b:2});//cr

javascript - 我可以在返回的文档中使用 mongoose 进行查询吗?

我有一个这样的模式...varTerritorySchema=newSchema({user:Schema.Types.ObjectId,streets:[streets_schema])};varStreetsSchema=newSchema({name:String,odd:[block_schema],even:[block_schema],tags:[String])};需要能够通过用户ID找到领土。但是,然后我需要在返回的文档中的街道中按名称找到一条街道。这就是我正在做的...TerritoryModel.findOne({user:userId}).then(territo

mongodb - 如何在寻找嵌入式文档的 Mongoose 中执行更新插入?

SocialProfileSchema=newmongoose.Schemasource:type:Stringenum:['twitter','facebook']lowercase:trueuser_id:Stringprofile_url:Stringprimary:type:Booleandefault:trueadded_on:type:Stringdefault:Math.round(newDate().getTime()/1000.0)UserProfileSchema=newmongoose.SchemasocialProfiles:[SocialProfileSche

javascript - Mongoose 和字段类型 - 数字返回对象,字符串返回字符串?

假设您像这样定义一个Mongoose模式aSchema=newSchemacount:Numbertext:StringA=mongoose.model"A",aSchemadb=mongoose.connect"mongodb://localhost/test"a=newAa.count=99a.text="foo"a.save(err)->A.findByIda,(err,a)->console.logtypeofa.text,typeofa.count#printsstring,objectString类型的字段按预期运行。但是数字字段作为对象返回,这意味着它们需要在用于比较等之

javascript - Node.js + Mongoose ...控制流问题?

刚开始玩node.js,过去有一些JavaScript经验,但在服务器端方面的经验很少。我有以下代码,我正在寻找的功能也是使用Mongoose将产品添加到集合中,但是,如果使用“名称”找到重复项,则将产品作为嵌入式文档推送到重复,只有选择信息,例如产品ID、包信息等。我假设问题与findOne在第一次运行时没有找到任何重复项有关。如果我第二次运行它,它确实会检测到重复项,但到那时为时已晚。我不确定如何让findOne仅在上一次“保存”发生后才运行。functioninsertProduct(data){varproduct;Product.findOne({name:data.name

html - node.js , mongodb, mongoose, html 如何插入数据(在 index.html 中)?

varmongoose=require('mongoose')Schema=mongoose.Schema;mongoose.connect('mongodb://localhost/mydatabase');//connectdatabase/****DefineUserSchema***/varUserSchema=newSchema({user_id:String,email:String,base_location:Number,user_type:String,number_of_event:Number});mongoose.model('User',UserSchema)