mongodb-query

Count array elements that matches condition

强颜欢笑 提交于 2019-12-17 17:07:08
问题 I have a mongoDB collection called "conference" with an array of participants as below : [ { "_id" : 5b894357a0c84d5a5d221f25, "conferenceName" : "myFirstConference", "startDate" : 1535722327, "endDate" : 1535722420, "participants" : [ { "name" : "user1", "origin" : "internal", "ip" : "192.168.0.2" }, { "name" : "user2", "origin" : "external", "ip" : "172.20.0.3" }, ] }, ... ] I would like to get the following result : [ { "conferenceName" : "myFirstConference", "startDate" : 1535722327,

Append item to MongoDB document array in PyMongo without re-insertion

≡放荡痞女 提交于 2019-12-17 16:05:25
问题 I am using MongoDB as the back-end database for Python web application (PyMongo + Bottle). Users can upload files and optionally 'tag' these files during upload. The tags are stored as a list within the document, per below: { "_id" : ObjectId("561c199e038e42b10956e3fc"), "tags" : [ "tag1", "tag2", "tag3" ], "ref" : "4780" } I am trying to allow users to append new tags to any document. I came up with something like this: def update_tags(ref, new_tag) # fetch desired document by ref key as

MongoDB, MapReduce and sorting

半腔热情 提交于 2019-12-17 15:44:59
问题 I might be a bit in over my head on this as I'm still learning the ins and outs of MongoDB, but here goes. Right now I'm working on a tool to search/filter through a dataset, sort it by an arbitrary datapoint (eg. popularity) and then group it by an id. The only way I see I can do this is through Mongo's MapReduce functionality. I can't use .group() because I'm working with more than 10,000 keys and I also need to be able to sort the dataset. My MapReduce code is working just fine, except for

How to convert a pymongo.cursor.Cursor into a dict?

只愿长相守 提交于 2019-12-17 15:38:18
问题 I am using pymongo to query for all items in a region (actually it is to query for all venues in a region on a map). I used db.command(SON()) before to search in a spherical region, which can return me a dictionary and in the dictionary there is a key called results which contains the venues. Now I need to search in a square area and I am suggested to use db.places.find , however, this returns me a pymongo.cursor.Cursor class and I have no idea how to extract the venue results from it. Does

MongoDB C# Driver - Ignore fields on binding

半世苍凉 提交于 2019-12-17 15:35:31
问题 When using a FindOne() using MongoDB and C#, is there a way to ignore fields not found in the object? EG, example model. public class UserModel { public ObjectId id { get; set; } public string Email { get; set; } } Now we also store a password in the MongoDB collection, but do not want to bind it to out object above. When we do a Get like so, var query = Query<UserModel>.EQ(e => e.Email, model.Email); var entity = usersCollection.FindOne(query); We get the following error Element 'Password'

Mongodb query based on item at specific position in array

旧时模样 提交于 2019-12-17 14:57:01
问题 I want to run a query where an item at a specific position in an array needs to be compared. For example, consider the GeoJSON format for storing location data. //sample document from collection user { name: "Some name", location : { type: "Point", coordinates : [<Longitude>, <Latitude>] } } How would I query users located at a specific longitude? I cant seem to find anything in the documentation which can help me do the same. Queries I have tried: db.users.find({"location.coordinates[0]" :

Mongoose populate virtual with sort and limit

佐手、 提交于 2019-12-17 14:07:15
问题 I am trying to write a mongoose query that retrieves a group of assets along with the most recent transactions for those assets. The transactions are in a separate collection from the assets. To accomplish this, first I created a virtual array in the asset model to link the assets to the transactions. schema.virtual('transactions', { ref: 'transaction', localField: '_id', foreignField: '_asset', justOne: false }) Then I query using .populate in an node.js express controller (note the hard

Aggregating in local timezone in mongodb

↘锁芯ラ 提交于 2019-12-17 13:54:58
问题 I am building application in mongodb and nodejs that will be used in Italy . Italy timezone is +02:00 . This means if any one saving some data at 01:am of 11 July then it will be saved as 11:00 pm of 10 July as mongo saves date in UTC. We need to show date wise tx count. So I made group by query on date. But it shows that tx in previous day. What should be workaround for this. > db.txs.insert({txid:"1",date : new Date("2015-07-11T01:00:00+02:00")}) > db.txs.insert({txid:"2",date : new Date(

Update embedded object inside array inside array in MongoDB

▼魔方 西西 提交于 2019-12-17 11:51:58
问题 I have document like { id : 100, heros:[ { nickname : "test", spells : [ {spell_id : 61, level : 1}, {spell_id : 1, level : 2} ] } ] } I can't $set spell's level : 3 with spell_id : 1 inside spells that inside heros with nickname "test. I tried this query: db.test.update({"heros.nickname":"test", "heros.spells.spell_id":1}, {$set:{"heros.spells.$.level":3}}); Errror i see is can't append to array using string field name [spells] Thanks for help. 回答1: You can only use the $ positional operator

Update embedded object inside array inside array in MongoDB

落爺英雄遲暮 提交于 2019-12-17 11:51:12
问题 I have document like { id : 100, heros:[ { nickname : "test", spells : [ {spell_id : 61, level : 1}, {spell_id : 1, level : 2} ] } ] } I can't $set spell's level : 3 with spell_id : 1 inside spells that inside heros with nickname "test. I tried this query: db.test.update({"heros.nickname":"test", "heros.spells.spell_id":1}, {$set:{"heros.spells.$.level":3}}); Errror i see is can't append to array using string field name [spells] Thanks for help. 回答1: You can only use the $ positional operator