mongodb-query

mongo $slice query reverse index out of range

一个人想着一个人 提交于 2019-12-30 10:08:05
问题 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"

MongoDB sum arrays from multiple documents on a per-element basis

妖精的绣舞 提交于 2019-12-30 10:06:12
问题 I have the following document structure (simplified for this example) { _id : ObjectId("sdfsdf"), result : [1, 3, 5, 7, 9] }, { _id : ObjectId("asdref"), result : [2, 4, 6, 8, 10] } I want to get the sum of those result arrays, but not a total sum, instead a new array corresponding to the sum of the original arrays on an element basis, i.e. result : [3, 7, 11, 15, 19] I have searched through the myriad questions here and a few come close (e.g. this one, this one, and this one), but I can't

MongoDB Update (Insert a List of Item to an Array)

被刻印的时光 ゝ 提交于 2019-12-30 06:16:36
问题 I want to add several items to arrays of several rows in Mongo. How can I do this? I want to start with this: {'x': 'h', arr: [1,2,3] } {'x': 'h', arr: [1,3] } and add the array [6,8], where the x is equal to 'h': {'x': 'h', arr: [1,2,3,6,8] } {'x': 'h', arr: [1,6,8] } 回答1: I think what you are looking for is the $pushAll operator. Take a look here: http://docs.mongodb.org/manual/reference/operator/pushAll/#pushall 回答2: If you have a MongoDB collection named yourCollection and a record with

MongoDb aggregation $match error : “Arguments must be aggregate pipeline operators”

北城余情 提交于 2019-12-30 06:03:32
问题 I can get all stats of the site with aggregation but I want to it for a certain user, like $where . All stats: games.aggregate([{ $group: { _id: '$id', game_total: { $sum: '$game_amount'}, game_total_profit: { $sum: '$game_profit'}} }]).exec(function ( e, d ) { console.log( d ) }) When I try to use $match operator, I'm getting error : games.aggregate([{ $match: { '$game_user_id' : '12345789' }, $group: { _id: '$id', game_total: { $sum: '$game_amount'}, game_total_profit: { $sum: '$game_profit

Push object into array if the array exists otherwise create the array with object in MongoDB

﹥>﹥吖頭↗ 提交于 2019-12-30 05:58:12
问题 I have collection with a document like this: { _id : "abcd", name : "Tom", myArray : [ { field1 : "", field2 : "" } ] }, { _id : "efgh", name : "Jerry" } I have a new object for myArray . I want to write a query to update only one document. If the query matches the document with _id : "abcd" , then push the new object in to myArray field: { _id : "abcd", name : "Tom", myArray : [ { field1 : "", field2 : "" }, { // new object } ] } And if the query matches with _id : "efgh" create the field

Mongodb distinct on a array field with regex query?

纵然是瞬间 提交于 2019-12-30 05:03:25
问题 Basically i'm trying to implement tags functionality on a model. > db.event.distinct("tags") [ "bar", "foo", "foobar" ] Doing a simple distinct query retrieves me all distinct tags. However how would i go about getting all distinct tags that match a certain query? Say for example i wanted to get all tags matching foo and then expecting to get ["foo","foobar"] as a result? The following queries is my failed attempts of achieving this: > db.event.distinct("tags",/foo/) [ "bar", "foo", "foobar"

Using full text search with geospatial index on Mongodb

為{幸葍}努か 提交于 2019-12-30 03:13:05
问题 Let's say I want to develop an android app that allows a user to search a hotel that is closest to where you are located. This is very common on apps nowadays, like AirBnb for example. This is the dataset I'm using: { "name" : "The Most Amazing Hotel", "city" : "India", "type": "Point" "coord": [ -56.16082, 61.15392 ] } { "name" : "The Most Incredible Hotel", "city" : "India", "type": "Point" "coord": [ -56.56285, 61.34590 ] } { "name" : "The Fantastic GuestHouse", "city" : "India", "type":

MongoDB - Aggregation - To get unique items in array

a 夏天 提交于 2019-12-30 00:37:13
问题 Here's my MongoDB collection: { "_id" : ObjectId("515d8f53175b8ecb053425c2"), "category" : "Batteries", "products" : [ { "brand" : "Duracell", "item" : [ "AA", "AAA" ] }, { "brand" : "Everyday", "item" : [ "9V", "AA", "12V" ] } ] } The output that I need is 1) Unique list of all items {["AA", "AAA", "9V", "12V"]} and 2. unique list of items per product { "category" : "Batteries", "item": ["AA", "AAA", "9V", "12V"] } I'm very new to MongoDB, and I tried different aggregations functions and

Mongodb Query based on number of fields in a record

浪尽此生 提交于 2019-12-29 09:21:52
问题 I haven't been very good at Googling for this answer. I have around 115 different fields that might be in each record. Collection is the output of a mapreduce on an amazingly large dataset. Looks like this: {_id:'number1', value:{'a':1, 'b':2, 'f':5}}, {_id:'number2', value:{'e':2, 'f':114, 'h':12}}, {_id:'number3', value:{'i':2, 'j':22, 'z':12, 'za':111, 'zb':114}} Any ideas of how I might find records with 5 fields populated? 回答1: It's still not a nice query to run, but there is a slightly

find in array between dates in mongo

元气小坏坏 提交于 2019-12-29 09:14:34
问题 I have mongo documents: { id:1 time:[ 1, 2, 10 ] } { id:2 time:[ 1, 4, 8, 10 ] } I want find all documents, which have time between 3 and 5 or between 7 or 9 (document with id 2). I dont know how do this. I try: mongo.find( $or : [{time:{$gte:3, $lte:5}}, {time:{$gte:7, $lte:9}}] ) and mongo returns documents with id 1 and 2, but i need id2 with time[4, 8]. How to apply a condition to each element of the array, but NOT to entire array? p.s. Size array "time" maybe different 回答1: Try using the