mongodb-query

MongoDB sum arrays from multiple documents on a per-element basis

你。 提交于 2019-12-11 05:27:11
问题 I have the following document structure (simplified for this example) { _id : ObjectId("sdfsdf"), result : [1, 3, 5, 7, 9] }, { _id : ObjectId("asdref"), result : [2, 4, 6, 8, 10] } I want to get the sum of those result arrays, but not a total sum, instead a new array corresponding to the sum of the original arrays on an element basis, i.e. result : [3, 7, 11, 15, 19] I have searched through the myriad questions here and a few come close (e.g. this one, this one, and this one), but I can't

Update multiple docs in a collection using switch case

落花浮王杯 提交于 2019-12-11 05:17:29
问题 I use MongoDB native driver in my NodeJS application. I have a shifts collection in my database that I need to update. Sample docs in my shifts collection { "_id" : ObjectId("588425105560bd2ba0065fa4"), "from" : ISODate("2017-01-23T03:20:00.000Z"), "to" : ISODate("2017-01-23T06:20:00.000Z"), "jobId" : ObjectId("586efda790541421b0432897"), "hourlyRate" : 15 } { "_id" : ObjectId("588425105560bd2ba0065fa5"), "from" : ISODate("2017-01-25T03:20:00.000Z"), "to" : ISODate("2017-01-25T06:20:00.000Z")

Getting com.mongodb.MongoException$DuplicateKey in mongodb with java using upsert

邮差的信 提交于 2019-12-11 05:09:35
问题 I am not able to update the existing record with mongo db upsert using java. I wrote a query to find the record using id but when trying to update its throwing com.mongodb.MongoException$DuplicateKey exception. Sample data: {"_id" : ObjectId("5788bef4191fda5c9077af78"), "type" : "PRIVATE", "users" : [ { "_id" : "800", "Name" : "Jack" }, { "_id" : "530a7998-ba3f-4366-8d21-bb1ca688cfdb", "Name" : "Ashley" } ]} Java query Query query = new Query(); query.addCriteria(Criteria.where("_id").is(

sort in populate does not work (Mongoose)

巧了我就是萌 提交于 2019-12-11 05:07:39
问题 My MongoDB version 3.2, mongoose version is 4.6.0 These are my schemas: // chat const chatSchema = new mongoose.Schema({ users: [{ type: mongoose.Schema.Types.ObjectId, ref: 'User', required: true }], lastMessage: { type: mongoose.Schema.Types.ObjectId, ref: 'Message' } }); export const ChatModel = mongoose.model('Chat', chatSchema); // message const messageSchema = new mongoose.Schema({ user: { type: mongoose.Schema.Types.ObjectId, ref: 'User', required: true }, chat: { type: mongoose.Schema

Adding new element to array in MongoDb

无人久伴 提交于 2019-12-11 04:48:12
问题 I have a mongodb collection like this:- { "_id": ObjectId("52174bcb834806830e5447"), "roles": [ { "role": "admin" }, { "role": "user" } ] } I need to add a new 'role' to the roles array. Like this {"role": "guest" } . How do I do that? 回答1: You can do this with the $push-operator This should work: db.collection.update( { _id: ObjectId("52174bcb834806830e5447") }, { $push: { roles: { role: "guest" } } } ); 回答2: In addition, you may use $addToSet to avoid duplicates. 来源: https://stackoverflow

Sort the array in the document with MongoDB

不打扰是莪最后的温柔 提交于 2019-12-11 04:44:17
问题 I have data structure from mongoDb v 3.2 I will try make sorting date by month and sorting within statistic array through aggregation-framework Source data: "monthStart" : "2015-11", "monthEnd" : "2015-11", "date" : ISODate("2016-03-09T09:06:58Z"), "statistic" : [ { "name" : "site1.com", "ga" : "99999", "data" : { "desktop" : { "users" : NumberLong(25), "pageviews" : NumberLong(56789), } } }, { "name" : "site2.com", "ga" : "102", "data" : { "desktop" : { "users" : NumberLong(21), "pageviews"

Multiple Logical Conditions in MongoDB Select

假装没事ソ 提交于 2019-12-11 04:36:03
问题 I need to Query a Collection based on Multiple Condition My Collection { "_id" : ObjectId("575f4e2efd14481598fc0ebf"), "Emp_ID" : "100", "LastUpdate" : ISODate("2016-06-13T18:30:00.000Z") }, { "_id" : ObjectId("575f4e2efd14481598fc0ebf"), "Emp_ID" : "101", "LastUpdate" : ISODate("2016-06-14T06:33:00.000Z") }, ... () Now I need to Check the document is exist or not based on my below condition My C# Code: private static IMongoClient _client; private static IMongoDatabase _database; _client =

Mongo db conditional query

不羁的心 提交于 2019-12-11 04:35:59
问题 being a newbie to mongo, stuck in a conditional query: I want to perform a search on the basis of 3 criteria, first name, last name and email id: below query works perfect when all the fields exist: db.students.find( { $and: [ { first_name: /^Abc$/i }, { last_name: 'Xyz'},{email_id:'gd@he'} ]}) the problem is when I don't give an email id , the query dosen't returns any result as it considers the email id to be null and searches for the combination 'Abc Firtis null', where as I want the below

MongoDB Aggregation Query to Count

匆匆过客 提交于 2019-12-11 04:28:26
问题 I am using Nodejs and MongoDB with expressjs and mongoose library & How can I get mutual between another user. Here are the Schema I use. const UsersSchema = new mongoose.Schema({ username: { type: String }, email: { type: String }, date_created: { type: Date }, last_modified: { type: Date } }); const FollowSchema = new Schema({ follower: { type: Schema.Types.ObjectId, ref: 'User', required: true }, following: { type: Schema.Types.ObjectId, ref: 'User', required: true }, date_created: { type:

Mongo 3.6 aggregation lookup with multiple conditions

风格不统一 提交于 2019-12-11 04:25:29
问题 Suppose I have a Mongo DB with only one collection data . In this collection, I have the following documents: { "type": "person", "value": { "id": 1, "name": "Person 1", "age": 10 } }, { "type": "person", "value": { "id": 2, "name": "Person 2", "age": 20 } }, { "type": "prescription", "value": { "drug": "Bromhexine", "patient": 2 } }, { "type": "prescription", "value": { "drug": "Aspirin", "patient": 1 } } With those records, I'd like to make a JOIN between documents with "type": person and