mongodb-query

How to $push a field depending on a condition?

人盡茶涼 提交于 2019-12-12 19:23:27
问题 I'm trying to conditionally push a field into an array during the $group stage of the MongoDB aggregation pipeline. Essentially I have documents with the name of the user, and an array of the actions they performed. If I group the user actions like this: { $group: { _id: { "name": "$user.name" }, "actions": { $push: $action"} } } I get the following: [{ "_id": { "name": "Bob" }, "actions": ["add", "wait", "subtract"] }, { "_id": { "name": "Susan" }, "actions": ["add"] }, { "_id": { "name":

Mongoose find element in array

别说谁变了你拦得住时间么 提交于 2019-12-12 19:15:17
问题 I just started with Mongoose and having issues finding elements... My schema contains an array of subregions from which I want to find the matching one by its code . The schema is the following: var schema = { name: { type: String, required: true } ... subRegions: [{ name: { type: String, required: true }, code: { type: String, required: true } }] ... }; I came up with find({ subRegions: { "$in": [{ code: regionCode }] } }).exec(...) but this is not working... 回答1: Your terminology is off as

sort array in query and project all fields

女生的网名这么多〃 提交于 2019-12-12 19:09:11
问题 I would like to sort a nested array at query time while also projecting all fields in the document. Example document: { "_id" : 0, "unknown_field" : "foo", "array_to_sort" : [ { "a" : 3, "b" : 4 }, { "a" : 3, "b" : 3 }, { "a" : 1, "b" : 0 } ] } I can perform the sorting with an aggregation but I cannot preserve all the fields I need. The application does not know at query time what other fields may appear in each document, so I am not able to explicitly project them. If I had a wildcard to

How to retrieve distinct keys inside an object in MongoDB

為{幸葍}努か 提交于 2019-12-12 19:04:43
问题 I have this in MongoDB: { "_id" : ObjectId("58fb35531eb5df245d5d434f"), "name" : "d1.html", "indexation" : { "Citroen" : 1, "color" : 1, "Marca" : 1, "rojo" : 1 } } { "_id" : ObjectId("58fb35531eb5df245d5d4350"), "name" : "d2.html", "indexation" : { "blancos" : 1, "ocasión" : 1, "Madrid" : 1, "Coches" : 1, "rojo" : 1, "coches" : 1 } } { "_id" : ObjectId("58fb35531eb5df245d5d4351"), "name" : "d3.html", "indexation" : { "rojos" : 1, "Ocasión" : 1, "marcas" : 1, "Madrid" : 1, "blancas" : 1,

How to update a document in MongoDB using ObjectID in Java

旧街凉风 提交于 2019-12-12 19:03:53
问题 What I am trying to accomplish here is pretty simple. I am trying to update a single document in MongoDB collection. When I look up the document using any field, such as "name", the update query succeeds. Here is the query: mongoDB.getCollection("restaurants").updateOne( new BasicDBObject("name", "Morris Park Bake Shop"), new BasicDBObject("$set", new BasicDBObject("zipcode", "10462")) ); If I try to lookup the document with the ObjectId, it never works as it doesn't match any document.

Retrieve sub-documents that match with maximum value in the array

半腔热情 提交于 2019-12-12 18:24:34
问题 I have the following data structure in mongodb [ { "id" : "unique id 1", "timeStamp" : "timeStamp", "topicInfo" : [ { topic : "topic1", offset : "offset number", time: 1464875267637 }, { topic : "topic2", offset : "offset number", time: 1464875269709 }, { topic : "topic3", offset : "offset number", time : 1464875270849 } ] }, { "id" : "unique id 2", "timeStamp" : "timeStamp", "topicInfo" : [ { topic : "15", offset : "offset number", time : 1464875271884 }, { topic : "topic2", offset : "offset

Filter Array Content to a Query containing $concatArrays

孤者浪人 提交于 2019-12-12 17:28:56
问题 Given this function, I have a data set that I am querying. The data looks like this: db.activity.insert( { "_id" : ObjectId("5908e64e3b03ca372dc945d5"), "startDate" : ISODate("2017-05-06T00:00:00Z"), "details" : [ { "code" : "2", "_id" : ObjectId("5908ebf96ae5003a4471c9b2"), "walkDistance" : "03", "jogDistance" : "01", "runDistance" : "08", "sprintDistance" : "01" } ] } ) db.activity.insert( { "_id" : ObjectId("58f79163bebac50d5b2ae760"), "startDate" : ISODate("2017-05-07T00:00:00Z"),

Select document having particular key value pair but not having other key value pair

元气小坏坏 提交于 2019-12-12 17:09:56
问题 I have a collection named "collection". It has 2 documents shown below - Document A { "genericParams" : [ { "key" : "sms_email_count", "value" : 3 }, { "key" : "first_sms_email_time", "value" : NumberLong("1450691202568") }, { "key" : "second_sms_email_time", "value" : NumberLong("1450691202568") }, { "key" : "third_sms_email_time", "value" : NumberLong("1450691202568") }, { "key" : "manual_refund_processed", "value" : "false" } ] } Document B { "genericParams" : [ { "key" : "sms_email_count"

alternate to $strLenCP field in mongoDB 3.0[prior versions]

此生再无相见时 提交于 2019-12-12 17:00:56
问题 I'm currently using mongo 3.0v. I've a requirement to find the length of each string in the result from aggregate command. For eg: db.getCollection('temp').find() [ {"key": "value1"}, {"key": "value2" }, {"key": "valuee2"} ] This query gives length of key field db.getCollection('temp').aggregate([{ $project: { "strLength": {"$strLenCP": "$key"} } }]) like [ {"strLength": 6}, {"strLength": 6}, {"strLength": 7} ] But "$strLenCP" key is not supported in prior 3.4 versions. So is there any

Save a subset of MongoDB(3.0) collection to another collection in Python

微笑、不失礼 提交于 2019-12-12 15:13:47
问题 I found this answer - Answer link db.full_set.aggregate([ { $match: { date: "20120105" } }, { $out: "subset" } ]); I want do same thing but with first 15000 documents in collection, I couldn't find how to apply limit to such query (I tried using $limit : 15000 , but it doesn't recognize $limit) also when I tried - db.subset.insert(db.full_set.find({}).limit(15000).toArray()) there is no function toArray() for output type cursor . Guide me how can I accomplish it? 回答1: Well, in python, this is