mongodb-query

Print document value in mongodb shell

蹲街弑〆低调 提交于 2020-08-22 12:37:45
问题 I want to print the value for this JSON document in the mongo shell. Like a simple console output, without creating a new collection or document. Thanks in advance 回答1: I found a solution, by using .forEach() to apply a JavaScript method: db.widget.find( { id : "4" }, {quality_level: 1, _id:0} ).forEach(function(x) { print(x.quality_level); }); 回答2: db.collection.find() returns a cursor . There are a bunch of methods for the cursor object which can be used to get the cursor information,

Mongo geoWithin error: Polygon coordinates must be an array

不打扰是莪最后的温柔 提交于 2020-08-10 23:17:49
问题 I have a data set with geo points. { _id ...other fields... location: { type: "Point", coordinates: [0,0]}} What I have been attempting to do is filter out and delete any documents that have points that are in water bodies. I downloaded and converted a shape file of the earth's water bodies and stored that in a separate collection in the same database. I have been trying to use Mongo's geoWithin function, but am not able to get it to work for me when I specify the water body's polygon

Incorrect count from aggregation query

我只是一个虾纸丫 提交于 2020-08-09 08:49:29
问题 In the following document collection, I am trying to find the total words of unique sentences. The total words must come out as 5 (hello\nworld, how are you?) + 5 (hello world, I am fine) + 3(Is it raining?) + 5(Look at the beautiful tiger!) = 18 [ { "sourceList": [ { "source": "hello\nworld, how are you?", "_id": ObjectId("5f0eb9946db57c0007841153") }, { "source": "hello world, I am fine", "_id": ObjectId("5f0eb9946db57c0007841153") }, { "source": "Is it raining?", "_id": ObjectId(

Incorrect count from aggregation query

烂漫一生 提交于 2020-08-09 08:46:24
问题 In the following document collection, I am trying to find the total words of unique sentences. The total words must come out as 5 (hello\nworld, how are you?) + 5 (hello world, I am fine) + 3(Is it raining?) + 5(Look at the beautiful tiger!) = 18 [ { "sourceList": [ { "source": "hello\nworld, how are you?", "_id": ObjectId("5f0eb9946db57c0007841153") }, { "source": "hello world, I am fine", "_id": ObjectId("5f0eb9946db57c0007841153") }, { "source": "Is it raining?", "_id": ObjectId(

Get total users list along with the active users in mongodb matched on OR condition

不想你离开。 提交于 2020-08-08 05:13:25
问题 Table : user: A, active_1 : "true", active_2 : "false", is_user : "true" user: B, active_1 : "false", active_2 : "true", is_user : "true" user: C, active_1 : "false", active_2 : "false", is_user : "true" Expected Output: { "_id" : null, "total" : 3, "count" : 2 } I need to check either the active_1 or active_2 is true and get the output as like total which indicates the total no.of users which is A,B and C. The count indicates the users who have either active_1 or active_2 is true. It should

MongoDB: Reduce array of objects into a single object by computing the average of each field

橙三吉。 提交于 2020-08-07 05:27:46
问题 I use the MongoDB aggregation API to aggregate some data daily. The result of this aggregation is of this format: [ { aggDate: '2019-05-23', results: [ { foo: 0.58, bar: 0.42 }, { foo: 0.32, bar: 0.98 } ] } ] The aggregation on the date is fine, but now I would like to aggregate the objects in the results array. The result of this aggregation should be of the following format: [ { aggDate: '2019-05-23', result: { foo: 0.45 // avg of all the `foo`, here: (0.58 + 0.32) / 2 bar: 0.7 // avg of

MongoDB: Reduce array of objects into a single object by computing the average of each field

╄→尐↘猪︶ㄣ 提交于 2020-08-07 05:26:21
问题 I use the MongoDB aggregation API to aggregate some data daily. The result of this aggregation is of this format: [ { aggDate: '2019-05-23', results: [ { foo: 0.58, bar: 0.42 }, { foo: 0.32, bar: 0.98 } ] } ] The aggregation on the date is fine, but now I would like to aggregate the objects in the results array. The result of this aggregation should be of the following format: [ { aggDate: '2019-05-23', result: { foo: 0.45 // avg of all the `foo`, here: (0.58 + 0.32) / 2 bar: 0.7 // avg of

MongoDB Date range query for past hour

久未见 提交于 2020-08-06 11:58:13
问题 I'm trying to write MongoDB query which will be return data from one hour ago. There is a column time with timestamps ( "time" : NumberLong("1471953787012") ) and this is how it looks in SQL: select name from table where time between (NOW() - INTERVAL 1 HOUR) AND (NOW()) How do I write a MongoDB query to find a date range from one hour ago? I'm trying with new Date() function but it doesn't work. Does anyone know what I'm doing wrong? db.coll.find({ "time" : { $lt: new Date(), $gte: new Date

MongoDB count and results

江枫思渺然 提交于 2020-07-30 07:29:52
问题 I would really appreciate if someone could help me with something: I need to make a normal query to the database but, as my collection is very large (10000 documents) I need to do the query and use $limit and $skip . That part I solved but now I want to have a count to all the documents, even if the returned ones are less. The output should be something like this: { count: 1150, data: [/*length of 50*/] } Could anyone help please? Thank you very much. 回答1: Since you mentioned you are making a

MongoDb Aggregation: How can I group an array-1 based on another array-2 when given array-1 and array-2?

人盡茶涼 提交于 2020-07-28 04:24:32
问题 EDIT: My original question was MongoDb Aggregation: Can you $unwind an input document variable in the pipline of a $lookup stage? Consider the code below: {$lookup: { from:"mydoc", let: {"c":"$myArray"}, pipeline: [ {$unwind: "$$c"}, ] as:"myNewDoc" }} How would I unwind c if I wanted to? /////END OF ORIGINAL QUESTION -----EDIT----- From Tom Slabbaert's comment we now know that it is possible to $unwind an input document variable in the pipline of a $lookup stage. But it is not recommended.