aggregation-framework

Before $unwind check if sub document is not empty

ⅰ亾dé卋堺 提交于 2019-12-31 03:49:26
问题 I have a Job schema that has job_title, job_location, salary, etc ApplicationSchema is embedded sub document into Job document and it stores all the applications that this particular job has received Here is how Job Schema looks const jobSchema = new Schema({ job_title : { type : String, required : true }, job_location : { type : String, }, salary : { type : Number }, applications: [ApplicationSchema], companyId : { type: mongoose.Schema.Types.ObjectId, ref: 'Company' } },{timestamps : true})

How to remove duplicates with a certain condition in mongodb?

风流意气都作罢 提交于 2019-12-31 03:34:26
问题 For example, I have the following documents in my collection: { "_id" : "GuqXmAkkARqhBDqhy", "beatmapset_id" : "342537", "version" : "MX", "diff_approach" : "5", "artist" : "Yousei Teikoku", "title" : "Kokou no Sousei", "difficultyrating" : "3.5552737712860107" } { "_id" : "oHLT7KqsB7bztBGvu", "beatmapset_id" : "342537", "version" : "HD", "diff_approach" : "5", "artist" : "Yousei Teikoku", "title" : "Kokou no Sousei", "difficultyrating" : "2.7515676021575928" } { "_id" : "GbotZfrPEwW69FkGD",

Aggregation issue with Mongo 3.6

▼魔方 西西 提交于 2019-12-31 03:33:11
问题 I was using the aggregate function without any problem connecting a 3.4 mongodb. When I change to a 3.6 db, I've got the message: The 'cursor' option is required, except for aggregate with the explain argument. Sorry if it's already posted. I am unable to find any solutions 回答1: In mongo 3.6 there has been changes while using aggregate you have to use cursor, unless you include the explain option, you must specify the cursor option. I faced same error as you were facing. Now you have to do it

Conditional $lookup in MongoDB?

∥☆過路亽.° 提交于 2019-12-31 00:58:26
问题 I have two collections in MongoDB 3.6: users: [ {name: "John", allowedRoles: [1, 2, 3]}, {name: "Charles", allowedRoles: [1]}, {name: "Sarah", isAdmin: true} ] roles: [ {_id: 1, name: "Foo", description: "This role allows to foo the blargs"}, {_id: 2, name: "Bar", description: "..."}, {_id: 3, name: "Doh", descripcion: "..."} ] I'm very new to MongoDB; I just figured out how to query an user and join all the data from his roles, using the $lookup aggregation stage: db.users.aggregate([{ "

Get distinct ISO dates by days, months, year

感情迁移 提交于 2019-12-31 00:46:32
问题 I want to get a distinct set of years and months for all document objects in my MongoDB. For example, if documents have dates: 2015/08/11 2015/08/11 2015/08/12 2015/09/14 2014/10/30 2014/10/30 2014/08/11 Return unique months and years for all documents, ex: 2015/08 2015/09 2014/10 2014/08 Schema snippet: var myObjSchema = mongoose.Schema({ date: Date, request: { ... I tried using distinct against schema field date : db.mycollection.distinct('date', {}, {}) But this gave duplicate dates.

How can I get the lowest values in a MongoDB collection?

三世轮回 提交于 2019-12-30 23:32:14
问题 I have a MongoDB collection called product which has the following documents as seen below. { "product" : "Milk", "barcode" : 12345, "price" : 100, "store" : "BestBuy" }, { "product" : "Milk", "barcode" : 12345, "price" : 100, "store" : "WalMart" }, { "product" : "Milk", "barcode" : 12345, "price" : 130, "store" : "Target" }, { "product" : "Milk", "barcode" : 12345, "price" : 500, "store" : "Game" } I wish to query the collection and only return documents that have the lowest price e.g {

Mongodb convert multiple objects to an array, in place, permanently

♀尐吖头ヾ 提交于 2019-12-30 14:19:49
问题 I have a collection with documents in this form: { "name" : "John Smith", "store_affiliation" : { "stores" : { "ABCD" : { "role" : "General Manager", "startdate" : ISODate("1970-01-01T00:00:00.000+0000"), "enddate" : ISODate("1980-01-01T00:00:00.000+0000"), "permissions" : "GM" }, "1234" : { "role" : "Owner", "startdate" : ISODate("1970-01-01T00:00:00.000+0000"), "enddate" : null, "permissions" : "ALL" }, "4321" : { "role" : "Owner", "startdate" : ISODate("1990-01-01T00:00:00.000+0000"),

Mongodb convert multiple objects to an array, in place, permanently

你说的曾经没有我的故事 提交于 2019-12-30 14:19:15
问题 I have a collection with documents in this form: { "name" : "John Smith", "store_affiliation" : { "stores" : { "ABCD" : { "role" : "General Manager", "startdate" : ISODate("1970-01-01T00:00:00.000+0000"), "enddate" : ISODate("1980-01-01T00:00:00.000+0000"), "permissions" : "GM" }, "1234" : { "role" : "Owner", "startdate" : ISODate("1970-01-01T00:00:00.000+0000"), "enddate" : null, "permissions" : "ALL" }, "4321" : { "role" : "Owner", "startdate" : ISODate("1990-01-01T00:00:00.000+0000"),

Return document in each group with max value using MongoDB

限于喜欢 提交于 2019-12-30 11:34:09
问题 Given a dataset: {_id: 0, type: 'banana', amount: 5} {_id: 1, type: 'banana', amount: 3} {_id: 2, type: 'apple', amount: 8} {_id: 3, type: 'apple', amount: 2} What is the most efficient way of getting only the records of the same type , that has the highest amount ? The expected result is: {_id: 0, type: 'banana', amount: 5} {_id: 2, type: 'apple', amount: 8} Right now I'm doing it this way, but it seems kinda silly: collection.aggregate([ { $sort: { 'amount': -1 } }, { $group: { _id: '$type'

Compare arrays and Return the Difference

只愿长相守 提交于 2019-12-30 10:57:55
问题 I have an array A in memory created at runtime and another array B saved in a mongo database. How can I efficiently get all the elements from A that are not in B? You can assume that the array stored in mongodb is several orders of magnitude bigger than the array created at runtime, for that reason I think that obtaining the full array from mongo and computing the result would not be efficient, but I have not found any query operation in mongo that allows me to compute the result I want. Note