mongodb-query

MongoDB full text search - matching words and exact phrases

与世无争的帅哥 提交于 2019-12-06 14:29:38
问题 I'm currently having some issues with the full text search functionality in MongoDB. Specifically when trying to match exact phrases. I'm testing out the functionality in the mongo shell, but ultimately I'll be using Spring Data MongoDB with Java. So I first tried running this command to search for the words "delay", "late" and the phrase "on time" db.mycollection.find( { $text: { $search: "delay late \"on time\"" } }).explain(true); And the resulting explain query told me: "parsedTextQuery"

MongoDB - Index not being used when sorting and limiting on ranged query

怎甘沉沦 提交于 2019-12-06 13:49:16
I'm trying to get a sorted list of items using a ranged query on a collection containing bulletin-board data. The data structure of a "thread" document is: { "_id" : ObjectId("5a779b47f4fa72412126526a"), "title" : "necessitatibus tincidunt libris assueverit", "content" : "Corrumpitvenenatis cubilia adipiscing sollicitudin", "flagged" : false, "locked" : false, "sticky" : false, "lastPostAt" : ISODate("2018-02-05T06:35:24.656Z"), "postCount" : 42, "user" : ObjectId("5a779b46f4fa72412126525a"), "category" : ObjectId("5a779b31f4fa724121265164"), "createdAt" : ISODate("2018-02-04T23:46:15.852Z"),

Combining multiple sub-documents into a new doc in mongo

扶醉桌前 提交于 2019-12-06 12:38:11
I am trying to query multiple sub-documents in MongoDB and return as a single doc. I think the aggregation framework is the way to go, but, can't see to get it exactly right. Take the following docs: { "board_id": "1", "hosts": [{ "name": "bob", "ip": "10.1.2.3" }, { "name": "tom", "ip": "10.1.2.4" }] } { "board_id": "2", "hosts": [{ "name": "mickey", "ip": "10.2.2.3" }, { "name": "mouse", "ip": "10.2.2.4" }] } { "board_id": "3", "hosts": [{ "name": "pavel", "ip": "10.3.2.3" }, { "name": "kenrick", "ip": "10.3.2.4" }] } Trying to get a query result like this: { "hosts": [{ "name": "bob", "ip":

How to get average value from a hashmap in MongoDB?

允我心安 提交于 2019-12-06 12:26:09
问题 I have a time data in my Mongo database. Each document equal a minute and contain 60 seconds as objects with value for each. How to get average value of all seconds in one minute? A document looking like that: { "_id" : ObjectId("55575e4062771c26ec5f2287"), "timestamp" : "2015-05-16T18:12:00.000Z", "values" : { "0" : "26.17", "1" : "26.17", "2" : "26.17", ... "58" : "24.71", "59" : "25.20" } } 回答1: You could take two approaches here: Changing the schema and use the aggregation framework to

MongoDB: Update a field of an item in array with matching another field of that item

本小妞迷上赌 提交于 2019-12-06 12:25:18
I have a data structure like this: We have some centers . A center has some switches . A switch has some ports . { "_id" : ObjectId("561ad881755a021904c00fb5"), "Name" : "center1", "Switches" : [ { "Ports" : [ { "PortNumber" : 2, "Status" : "Empty" }, { "PortNumber" : 5, "Status" : "Used" }, { "PortNumber" : 7, "Status" : "Used" } ] } ] } All I want is to write an Update query to change the Status of the port that it's PortNumber is 5 to "Empty". I can update it when I know the array index of the port (here array index is 1) with this query: db.colection.update( // query { _id: ObjectId(

MongoDB Group and Subtract Values from Different Documents

左心房为你撑大大i 提交于 2019-12-06 12:20:06
问题 In my application I have the following documents { "timestamp": ISODate("2015-09-17T21:14:35.0Z"), "sensor": "gas-in", "value": 2.5, }, { "timestamp": ISODate("2015-09-17T21:14:35.0Z"), "sensor": "gas-out", "value": 2.0, }, { "timestamp": ISODate("2015-09-17T21:20:35.0Z"), "sensor": "gas-in", "value": 6.3, }, { "timestamp": ISODate("2015-09-17T21:20:35.0Z"), "sensor": "gas-out", "value": 0.8, } ...etc... How can I return the difference (gas-in) - (gas-out) grouped per timestamp ? I am trying

MongoDB Best way to pair and delete sequential database entries

坚强是说给别人听的谎言 提交于 2019-12-06 12:18:51
问题 Okay so let's say I'm making a game of blind war! Users A & B have x amount of soldiers There are currently 0 DB docs. User A sends 50 soldiers making a DB doc User B sends 62 soldiers after user A! This creates a new DB doc. I need the most effective/scalable way to lookup user A's doc, compare it to User B's doc then delete both docs! (After returning the result of course) Here's the problem! I could potentially have 10,000+ users sending soldiers at relatively the same time! How can I

Geonear sort by distance and time

妖精的绣舞 提交于 2019-12-06 12:00:37
问题 I have the following data: { "_id" : ObjectId("55a8c1ba3996c909184d7a22"), "uid" : "1db82e8a-2038-4818-b805-76a46ba62639", "createdate" : ISODate("2015-07-17T08:50:02.892Z"), "palce" : "aa", "sex" : 1, "longdis" : 1, "location" : [ 106.607312, 29.575281 ] } { "_id" : ObjectId("55a8c1ba3996c909184d7a24"), "uid" : "1db82e8a-2038-4818-b805-76a46ba62639", "createdate" : ISODate("2015-07-17T08:50:02.920Z"), "palce" : "bbb", "sex" : 1, "longdis" : 1, "location" : [ 106.589896, 29.545098 ] } { "_id"

How to Count Matched array elements

瘦欲@ 提交于 2019-12-06 11:18:17
I have a collection and each document in that collection has an array field countries . I want to select all documents which include any of below countries: China, USA, Australia And the output should show the number of above countries each document has. I use below aggregate command: db.movies.aggregate([ { $match: { countries: { $in: ["USA", 'China', 'Australia'] } } }, { $project: { countries: {$size: '$countries'} } } ]); it doesn't work as expected. It shows the number of all countries in the document who has the above-listed country. For example, if a document has China, Japan in its

How to efficiently page batches of results with MongoDB

淺唱寂寞╮ 提交于 2019-12-06 10:51:34
I am using the below query on my MongoDB collection which is taking more than an hour to complete. db.collection.find({language:"hi"}).sort({_id:-1}).skip(5000).limit(1) I am trying to to get the results in a batch of 5000 to process in either ascending or descending order for documents with "hi" as a value in language field. So i am using this query in which i am skipping the processed documents every time by incrementing the "skip" value. The document count in this collection is just above 20 million. An index on the field "language" is already created. MongoDB Version i am using is 2.6.7 Is