草庐IT

mongodb - 计算每个文档的子文档

我试图只计算每个文档在Mongo中的子文档数。使用db.users.find().length()可以轻松获取集合中的文档。我想做类似db.users.projects.find().length()的事情。我该怎么做?编辑:我想要的结果是知道每个用户有多少个项目......所以像这样:{_id:123,projects:4}示例用户文档:{_id:{$oid:123},username:johnsmith,projects:[{$oid:456},{$oid:789}]} 最佳答案 根据@n9code,您将需要聚合框架。但是,您可

node.js - 带有子文档的 Mongoose 模式字段名称和类型

我正在从我的架构中构建字段名称及其类型的对象:varret={};ThisCollection.schema.eachPath(function(path){ret[path]=ThisCollection.schema.path(path).instance;});这很好,除非架构中有嵌套数组。我似乎无法弄清楚如何访问子文档及其字段类型。我试过:ThisCollection.schema.path("dot.notation").instance在递归函数中构建更深路径的点符号名称。这似乎不起作用。示例模式:varPerson=newSchema({person_code:Strin

java - Spring Boot数据与MongoDB——过滤子文档数组查询

我正在尝试使用Spring查询Mongo存储库并过滤数组子文档。我引用了howtofilterarrayinsubdocumentwithmongodb,但想知道是否有更合适的或java结构化的方法来使用Spring来做到这一点。我目前正在使用速记存储库接口(interface)表示法,但我得到的是未过滤数组的完整文档。PersonRepository.java@RepositorypublicinterfacePersonRepositoryextendsMongoRepository{ListfindByAddressZipCode(@Param("zip")intzip);}Pe

MongoDB 如何获取子文档属性等于某个值的不同子文档列表?

db.test.insert({'name':'outer','foos':[{'name':'a','type':'bar',},{'name':'a','type':'bar',},{'name':'z','type':'baz',},{'name':'z','type':'baz',},]})如何获得一个不同的foos列表,其中foo.type等于bar?我想找到:[{'name':'a','type':'bar'}]以下不起作用,而是为所有foo返回一个不同的值。db.test.distinct('foos',{'foos.type':'bar'})

mongodb - 更新 meteor mongo 中的子文档

我正在处理需要更新meteormongo中的子文档的查询。我的代码如下所示:Cases.update({"_id":doc._id,"notes._id":doc.noteid},{$set:{'notes.$':{'note':doc.note,'updatedBy':currentUser,'updatedAt':date}}});这确实有效,但是它确实删除了不在更新中的其他字段,例如“createdAt”和“date”。我这样做了,但我得到了相同的结果:Cases.update({"_id":doc._id,"notes._id":doc.noteid},{$set:{'note

mongodb - 当键是数字时如何访问子文档值

我有一个看起来像这样的mongo文档{"_id":ObjectId("58a18bdd7313101c38baa06d"),"date":"2017-02-12","previsions":{"1000":{"tmin":"3","tmax":"13"},"1090":{"tmin":"3","tmax":"13"},"1100":{"tmin":"-1","tmax":"5"},"1110":{"tmin":"3","tmax":"12"},"1120":{"tmin":"3","tmax":"14"}},"created_at":ISODate("2017-02-13T10:35:

MongoDB 更新最新的子文档

这是我的mongo文档..{"_id":ObjectId("5a69d0acb76d1c2e08e4ccd8"),"subscriptions":[{"sub_id":"5a56fd399dd78e33948c9b8e","invoice_id":"5a56fd399dd78e33948c9b8d"},{"sub_id":"5a56fd399dd78e33948c9b8e"}]}我想更新invoice_id并将其插入到子数组的最后一个元素中。我试过..排序:{$natural:-1},订阅.$.发票我想要的是......{"_id":ObjectId("5a69d0acb76d1c2e

node.js - 根据子文档日期查找即将发布的文档

假设我有一些MongoDB事件文档,每个文档都有许多在不同日期发生的session。我们可以将其表示为:db.events.insert([{_id:'5be9860fcb16d525543cafe1',name:'Past',host:'5be9860fcb16d525543daff1',sessions:[{date:newDate(Date.now()-1e8)},{date:newDate(Date.now()+1e8)}]},{_id:'5be9860fcb16d525543cafe2',name:'Future',host:'5be9860fcb16d525543daff2

javascript - 无法将 .push() 子文档放入 Mongoose 数组

我有一个MongooseJS架构,其中父文档引用一组子文档:varparentSchema=mongoose.Schema({items:[{type:mongoose.Schema.Types.ObjectId,ref:'Item',required:true}],...});为了测试,我想用一些虚拟值填充父文档上的项目数组,而不将它们保存到MongoDB:varitemModel=mongoose.model('Item',itemSchema);varitem=newitemModel();item.Blah="testdata";然而,当我尝试将此对象插入数组时,只有_id被存

javascript - 如何编写 Mongoose 查询来过滤子文档

我有一个名为“Users”的Mongoose模式,它有一个“Roles”子文档作为其变量之一,如下所示:varUserSchema=newmongoose.Schema({email:{type:String,required:true,unique:true},password:{type:String,required:true},roles:[{type:Number,ref:'Role'}]});varRoleSchema=newmongoose.Schema({_id:Number,name:{type:String,required:true,unique:true},de