mongodb-query

What is the difference between these two MongoDB queries?

混江龙づ霸主 提交于 2019-12-10 22:59:07
问题 Objective Find out possible differences in the following MongoDB queries and understand why one of them works and the other doesn't. Background A while ago I posted a questions asking for help regarding a MongoDB query: Using $push with $group with pymongo In that question my query didn't work, and I was looking for a way to fix it. I had a ton of help in the comments, and eventually found out the solution, but no one seems to be able to explain me why my first incorrect query doesn't work,

Mongodb Aggregate Nested Group with Recent Updated Document

冷暖自知 提交于 2019-12-10 22:24:02
问题 I have assigned person, status inside my collection simply like below. [ {"ASSIGN_ID": "583f84bce58725f76b322398", "SPEC_ID" : "58411771", "STATUS": 1, "UPDATE_DATE": ISODate("2016-12-21T04:10:23.000Z")}, {"ASSIGN_ID": "583f84bce58725f76b322398", "SPEC_ID" : "58411772", "STATUS": 4, "UPDATE_DATE": ISODate("2016-12-22T04:10:23.000Z")}, {"ASSIGN_ID": "583f84bce58725f76b322398", "SPEC_ID" : "58411774", "STATUS": 4, "UPDATE_DATE": ISODate("2016-12-23T04:10:23.000Z")}, {"ASSIGN_ID":

mongo-aggregation: apply regex grouping, string processing on $project

对着背影说爱祢 提交于 2019-12-10 22:07:09
问题 I'd like to apply some simple String manipulation when doing $project, is it possible to apply something like the following function on $project? : var themeIdFromZipUrl = function(zipUrl){ return zipUrl.match(/.*\/(T\d+)\/.*/)[1] }; I'm using the following query: db.clientRequest.aggregate( { $match: { "l": {$regex: ".*zip"}, "t": { "$gte": new Date('1/SEP/2013'), "$lte": new Date('7/OCT/2013') } } }, { $project: {"theme_url" : "$l", "_id": 0, "time": "$t"} }, { $group: { _id: { theme_url: "

How to add field with static value to mongodb find query?

我是研究僧i 提交于 2019-12-10 21:35:25
问题 Can we add some custom field with static value to mongodb find query? I am trying to add/append API request UId to all the queries that we are making to mongodb, so that we can map requests with slow queries from mongodb logs. I am doing it in aggregate queries by using '$literal' in projected fields. My Aggregate queries looks something like : db.test.aggregate({ $project: { "custom_id": { $literal: "uid" } .. } .. }) Also I can't include each field individually in projected fields and add

How to accumulate results (with forEach?) in MongoDB?

喜你入骨 提交于 2019-12-10 21:21:26
问题 Suppose I want to search through a collection, scan the returned result set and return some transformation of it. I've tried the following code: db.my_collection.find({timestamp : {$gt: 1343032491799}}, {_id:0,sid:1}).limit(20).forEach(function(element) { print(element.sid); }) Ok, it worked well. To the question: how can I accumulate the results ( sid s) into an array instead of just printing them? Update : ruby-style one-liner is preferred (but not required) of course 回答1: Call toArray on

Bulk update array of matching sub document in Mongodb

試著忘記壹切 提交于 2019-12-10 21:14:17
问题 I am running on Mongodb 3.6. Below is the structure of my document, which stores monthly rate information for list of products: { "_id": 12345, "_class": "com.example.ProductRates", "rates": [ { "productId": NumberInt(1234), "rate": 100.0, "rateCardId": NumberInt(1), "month": NumberInt(201801) }, { "productId": NumberInt(1234), "rate": 200.0, "rateCardId": NumberInt(1), "month": NumberInt(201802) }, { "productId": NumberInt(1234), "rate": 400.0, "rateCardId": NumberInt(2), "month": NumberInt

$setIntersection failed with array of subdocuments that are not in the collection

邮差的信 提交于 2019-12-10 19:52:16
问题 Consider the following document: { "item1" : [ { "a" : 1, "b" : 2 } ], "item2" : [ "a", "b" ] } The following query: db.test.aggregate([ { "$project": { "items": { "$setIntersection": [ "$item1", "$item2" ] } }} ]) returns the expected result: { "_id" : ObjectId("5710785387756a4a75cbe0d1"), "a" : [ ] } If the document looks like this: { "item2" : [ "a", "b" ] } Then: db.test.aggregate([ { "$project": { "a": { "$setIntersection": [ "$item2", [ "a" ] ] } } } ]) Yields: { "_id" : ObjectId(

Unique index in MongoDB

好久不见. 提交于 2019-12-10 19:25:54
问题 Can someone please help me with creation of unique index @ mongoDB for the following condition ? Suppose I have a schema like this, and I want a unique compound index on 1.path 2.verb 3."switches.name" 4."switches.value" { "path": "somepath", "verb": "GET", "switches": [ { "name": "id", "value":"123" }, { "name": "age", "value":"25" } ] } So if I try to insert { "path": "somepath", "verb": "GET", "switches": [ { "name": "id", "value":"123" }, { "name": "age", "value":"25" } ] } I should get

How can I sort by multiple fields in mongodb with Perl?

心已入冬 提交于 2019-12-10 19:09:21
问题 How can I get multiple sort in MongoDB with Perl? My current approach looks something like this: my $sort = {"is_instock" => -1, "ua" => 1}; my $resultSet = $collection ->find({moderated => 1, markers => {'$all'=>$obj->{markers}}}) ->sort($sort) ->limit(25); @{$result} = $resultSet->all; But, i got array sorted by one field(ua). What i did wrong? 回答1: The basic problem here is that a "hash" in Perl is ordered by "key" by default. In order to get the "order of insertion" you need to use Tie:

mongodb select from different databases

倖福魔咒の 提交于 2019-12-10 18:14:07
问题 I have about 200 mongodb databases. Every database has a collection called 'Group' and in this collection there is a field called 'meldingId'. Is it possible to make a one mongodb query which find all values in the different databases. (I managed to select the databases bij looping through the databases by selectDB($database_name)) 回答1: In Mongo shell, this can be done by using db.getSiblingDB() method to switch to admin database and get a list of the 200 databases by running the admin