mongodb-query

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

MongoDB - Storing date without timezone

末鹿安然 提交于 2019-12-31 01:27:28
问题 We have a simple application in which we have all user in same timezone & therefore we are not interested to store timezone information in mongo date object. Reason for such extreme step is we have multiple micro service using common database managed by different developers. Each of them requires to explicitly set timezone related stuff in query & forgetting same results in invalid dataset. Since currently MongoDB folks Mongo Data Types doesn't support storing dates without timezone. Just

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([{ "

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 updateMany with Conditional

痴心易碎 提交于 2019-12-30 11:15:14
问题 I have a large DB with various inconsistencies. One of the items I would like to clear up is changing the country status based on the population. A Sample of the data is: { "_id" : "D", "name" : "Deutschland", "pop" : 70000000, "country" : "Large Western" } { "_id" : "E", "name" : "Eire", "pop" : 4500000, "country" : "Small Western" } { "_id" : "G", "name" : "Greenland", "pop" : 30000, "country" : "Dependency" } { "_id" : "M", "name" : "Mauritius", "pop" : 1200000, "country" : "Small island"}

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

Reshape all the documents in the collection

扶醉桌前 提交于 2019-12-30 10:39:37
问题 I have the following structure in my documents: { "_id" : 1, "item" : { "name" : "abc", "price" : 10, "quantity" : 2, "date" : ISODate("2014-03-01T08:00:00Z") } } And I want to transform each document on this: { "_id" : 1, "name" : "abc", "price" : 10, "quantity" : 2, "date" : ISODate("2014-03-01T08:00:00Z") } In other words remove the embedded document but not the details! Thanks! 回答1: You can use the aggregation especially the $projectoperator for that. The $out operator let you write the

mongo $slice query reverse index out of range

余生长醉 提交于 2019-12-30 10:08:14
问题 The following query in mongo, behaves strange : db.items.findOne({},{ "List": { "$slice": [ skip, 3 ] }}) First: Instead of returning one object with ["_id","List"] keys only, it returns a full object. Second: if skip is negative and |skip| is higher than list.length then it returns the first three elements as though skip==0 I would expect for: { "_id" : ObjectId("542babf265f5de9a0d5c2928"), "List" : [ 1, 2, 3, 4, 5 ] "other" : "not_important" } query: db.items.findOne({},{ "List": { "$slice"