mongodb-query

How to rename/alias field(s) while fetching it from MongoDB through query using MongoDB-Node.JS native drive?

喜夏-厌秋 提交于 2019-12-07 03:59:13
问题 Consider the following code, which I am using to fetch the data from my local MongoDB server. var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); var db = new Db(

MongoDB 2.6 Index set up, query using $or, $in, with limit and sort

大兔子大兔子 提交于 2019-12-07 02:38:23
问题 I have a somewhat complex query that is very critical to my application. $cur = $col->find( array ( '$or' => array( array('owner' => $my_id), array('owner' => array('$in' => $friends), 'perm.type' => array('$in' => array('P', 'F'))), array('owner' => array('$in' => $friends), 'perm.list' => $my_id) ) ) )->limit(10)->skip(0)->sort(array('ca' => -1)); The intention is to find the first 10 posts, sorted by their create time in desc order, which are: a). made by myself, or b). made by my friends

Mongodb remove item from array

主宰稳场 提交于 2019-12-07 02:20:19
问题 I'm trying to remove an element from an array using Java and haven't been successful... I have a "emailsInApp" collection and inside I have this: { "_id" : "750afe", "list" : [ "John@gmail.com", "Mike@gmail.com" ] } { "_id" : "711850", "list" : [ "example@gmail.com" ] } It holds for each id the registered emails. What I would like to do is: given an id and an email, remove that email from that appId. This is what I have atm and when I run it it doesn't change the array at all: DBCollection

Using golang and mgo, how do I search for a range of values in MongoDB?

孤人 提交于 2019-12-07 02:11:48
问题 I worked through the example on the mgo homepage, but I'm struggling to find a way to query a range of values. The line: searchResults, searchErr = SearchReading(bson.M{"k": key, "t": { $gte: start, $lte: end } }, limit) fails with: line67: syntax error: unexpected $ line67: missing type in composite literal I left out the non-essential bits of code... type Reading struct { K string "k" T int64 "t" V float64 "v" } func SearchReading(q interface{}, limit int) (searchResults []Reading,

MongoDB regex matching trouble

孤者浪人 提交于 2019-12-07 01:03:02
问题 Here is my MongoDB shell session; > db.foo.save({path: 'a:b'}) WriteResult({ "nInserted" : 1 }) > db.foo.findOne() { "_id" : ObjectId("58fedc47622e89329d123ee8"), "path" : "a:b" } > db.foo.save({path: 'a:b:c'}) WriteResult({ "nInserted" : 1 }) > db.foo.find({path: /a:[^:]+/}) { "_id" : ObjectId("58fedc47622e89329d123ee8"), "path" : "a:b" } { "_id" : ObjectId("58fedc57622e89329d123ee9"), "path" : "a:b:c" } > db.foo.find({path: /a:[a-z]+/}) { "_id" : ObjectId("58fedc47622e89329d123ee8"), "path"

How to remove a document from the capped collection?

只愿长相守 提交于 2019-12-06 23:43:21
问题 When I try to remove a document in collection in MongoDB . It didn't remove because the collection is capped . My question is why? And is there a solution or other function can remove the document in this case? 回答1: No You cannot delete documents from a capped collection. And there is no possible workaround. The only thing you can do is drop() the collection. 回答2: You can not use remove function for capped collection. There are some restrictions on capped collections. Also you can refer this

Auto increment sequence in mongodb using java

蓝咒 提交于 2019-12-06 23:33:47
问题 Hi I want to auto increment _id in mongodb using java. I am completely new to this. In the document I found the solution like this: db.counters.insert( { _id: "userid", seq: 0 } ) function getNextSequence(name) { var ret = db.counters.findAndModify( { query: { _id: name }, update: { $inc: { seq: 1 } }, new: true } ); return ret.seq; } db.users.insert( { _id: getNextSequence("userid"), name: "Sarah C." } ) Can any one suggest how do I do this using java ? I am completely new to this. 回答1:

MongoDB $redact to filter out some elements of an array

断了今生、忘了曾经 提交于 2019-12-06 23:22:06
问题 I am trying to formulate a query over the sample bios collection http://docs.mongodb.org/manual/reference/bios-example-collection/: Retrieve all persons and their awards that they received before receiving a Turing award. I have come up with this query: db.bios.aggregate([ {$match: {"awards.award" : "Turing Award"}}, {$project: {"award1": "$awards", "award2": "$awards", "first_name": "$name.first", "last_name": "$name.last"}}, {$unwind: "$award1"}, {$match: {"award1.award" : "Turing Award"}},

subtraction in mongo query does not work?

丶灬走出姿态 提交于 2019-12-06 22:36:27
I have a collection as follow: { "_id" : ObjectId("5491d65bf315c2726a19ffe0"), "tweetID" : NumberLong(535063274220687360), "tweetText" : "19 RT Toronto @SunNewsNetwork: WATCH: When it comes to taxes, regulations, and economic freedom, is Canada more \"American\" than America? http://t.co/D?", "retweetCount" : 1, "source" : "<a href=\"http://twitter.com\" rel=\"nofollow\">Twitter Web Client</a>", "Date" : ISODate("2014-11-19T04:00:00.000Z"), "Added" : ISODate("2014-11-19T04:00:00.000Z"), "tweetLat" : 0, "tweetLon" : 0, "url" : "http://t.co/DH0xj0YBwD ", "sentiment" : 18, "quality" : 0.4,

How to query date range on the MongoDB collection where the ISO date is stored in string field?

邮差的信 提交于 2019-12-06 19:56:47
问题 Scenario : Consider I am having a collection called MyCollection , with following data: { "_id" : 'MyUniqueID_01' "CreatedTime" : "2013-12-01T14:35:00Z", "LastModifiedTime" : "2013-12-01T13:25:00Z" } Now I want to query the MongoDB database where the above mentioned kind of data is in huge number of documents. And my query is based on date range i.e. using $gt , $gte , $lt & $lte So my query may be something like: db.MyCollection.find({ 'CreatedTime': {$gt: '2013-05-25T09:29:40.572Z'}})