mongodb-query

$lookup on ObjectId's in an array

烈酒焚心 提交于 2020-01-15 07:14:08
问题 What's the syntax for doing a $lookup on a field that is an array of ObjectIds rather than just a single ObjectId? Example Order Document: { _id: ObjectId("..."), products: [ ObjectId("..<Car ObjectId>.."), ObjectId("..<Bike ObjectId>..") ] } Not Working Query: db.orders.aggregate([ { $lookup: { from: "products", localField: "products", foreignField: "_id", as: "productObjects" } } ]) Desired Result { _id: ObjectId("..."), products: [ ObjectId("..<Car ObjectId>.."), ObjectId("..<Bike ObjectId

mongo BadValue unknown operator: $or

谁都会走 提交于 2020-01-15 06:46:12
问题 The collection has one document: { "_id" : ObjectId("54b513933aca242d9915a787"), "carriers" : [ { "carrier" : ObjectId("54b54d223aca242d9915a788"), "carryingInterval" : { "from" : ISODate("2013-12-31T23:00:00.000Z"), "to" : null } } ] } I would like to make a criteria, that is looking for a certain carrier and range. The carryingInterval 's from and to fields could be a Date object or null . The null value means +/- infinity. My criteria is: { carriers: { $elemMatch: { carrier: ObjectId(

MongoDB: find value in Array with multiple criteria

蹲街弑〆低调 提交于 2020-01-15 04:16:26
问题 I'm trying to filter documents based on their price range, i have the following document structure example { "name": "test 1", "priceObject" : [ { "price" : { "value" : 1000 } }, { "price" : { "value" : 500 } }, { "price" : { "value" : 333 } } ] } i use aggregate to match documents that have at least one price that must be greater than 500 and less than 1000 { "$match": { "priceObject.price.value": { "$gt": 500, "$lt": 1000 } } } it return "test 1" document, although it should not, because

MongoDB: find value in Array with multiple criteria

偶尔善良 提交于 2020-01-15 04:16:08
问题 I'm trying to filter documents based on their price range, i have the following document structure example { "name": "test 1", "priceObject" : [ { "price" : { "value" : 1000 } }, { "price" : { "value" : 500 } }, { "price" : { "value" : 333 } } ] } i use aggregate to match documents that have at least one price that must be greater than 500 and less than 1000 { "$match": { "priceObject.price.value": { "$gt": 500, "$lt": 1000 } } } it return "test 1" document, although it should not, because

updating a value in an array in mongodb from java

懵懂的女人 提交于 2020-01-14 06:07:02
问题 I have couple of documens in mongodb as follow: { "_id" : ObjectId("54901212f315dce7077204af"), "Date" : ISODate("2014-10-20T04:00:00.000Z"), "Type" : "Twitter", "Entities" : [ { "ID" : 4, "Name" : "test1", "Sentiment" : { "Value" : 20, "Neutral" : 1 } }, { "ID" : 5, "Name" : "test5", "Sentiment" : { "Value" : 10, "Neutral" : 1 } } ] } Now I want to update the document that has Entities.ID=4 by adding (Sentiment.Value+4)/2 for example in the above example after update we have 12. I wrote the

updating a value in an array in mongodb from java

拈花ヽ惹草 提交于 2020-01-14 06:06:33
问题 I have couple of documens in mongodb as follow: { "_id" : ObjectId("54901212f315dce7077204af"), "Date" : ISODate("2014-10-20T04:00:00.000Z"), "Type" : "Twitter", "Entities" : [ { "ID" : 4, "Name" : "test1", "Sentiment" : { "Value" : 20, "Neutral" : 1 } }, { "ID" : 5, "Name" : "test5", "Sentiment" : { "Value" : 10, "Neutral" : 1 } } ] } Now I want to update the document that has Entities.ID=4 by adding (Sentiment.Value+4)/2 for example in the above example after update we have 12. I wrote the

MongoDB Custom sorting on two fields

荒凉一梦 提交于 2020-01-14 05:35:27
问题 In our MongoDB document, we have two fields, organisationId and employeeId . I want to show all the elements in a collection that have either of them matching the query parameters, so a basic OR. One condition for Sorting is that I require is that the documents that have both the fields matching the query parameters should occur first, and then the documents matching organisationName parameter, next. The idea is to show the data for the employee (i.e., you) first and then that of your

Use $or with $elemMatch and condition outside of Array

。_饼干妹妹 提交于 2020-01-13 19:37:43
问题 My basic structure is I have User object with a session object that contains a subjectId and an hourly price. User{ defaultHourly: Number, subjects{ [ id: String, hourly: Number ] } } I use elemMatch like this: query.elemMatch("subjects", { "id": { $in: subjects }, "$or": [ { "hourly": { $eq: null } }, // this is my issue { "$and": [ { "hourly": { $ne: null } }, { "hourly": { $gte: price.low, $lte: price.high } } ] } ] }); the first part of elemMatch makes sure that the subjectId I want is in

Mongodb query - apply condition only if field exists

大城市里の小女人 提交于 2020-01-13 09:12:56
问题 is there a way to make a query such that if certain field exists it will apply WHERE like condition on that field and if it pass will add that document to the result. If such field doesn't exist, it will immediately add that document to the result? 回答1: How about something like this: db.stackoverflow.find({ $or: [ { howmuch: { $exists:false } }, { howmuch:5 } ]}) In the stackoverflow collection, this will find all documents that do not have the howmuch field plus all documents that do have

Mongodb query - apply condition only if field exists

大城市里の小女人 提交于 2020-01-13 09:11:47
问题 is there a way to make a query such that if certain field exists it will apply WHERE like condition on that field and if it pass will add that document to the result. If such field doesn't exist, it will immediately add that document to the result? 回答1: How about something like this: db.stackoverflow.find({ $or: [ { howmuch: { $exists:false } }, { howmuch:5 } ]}) In the stackoverflow collection, this will find all documents that do not have the howmuch field plus all documents that do have