mongodb-php

Handling unwind for the non existing embedded document [duplicate]

只愿长相守 提交于 2019-12-02 06:27:21
This question already has an answer here: $unwind empty array 2 answers I am performing aggregation on employees table to fetch some hostel details with projection like $query = ['_id' => new MongoDB\BSON\ObjectID($this->EmployeeId)]; $pipeline = array( ['$match' => $query], [ '$addFields'=> [ 'assigned_master_keys'=> [ '$cond'=> [ 'if'=> [ '$ne'=> [ [ '$type'=> '$assigned_master_keys' ], 'array' ] ], 'then'=> [], 'else'=> '$assigned_master_keys' ] ] ]], ['$unwind'=> '$assigned_master_keys'], ['$lookup' => [ 'from' => 'HostelTbl', 'let' => [ 'hostelid' => '$assigned_master_keys.hostel_id' ],

Percentage of OR conditions matched in mongodb

这一生的挚爱 提交于 2019-12-01 12:39:32
问题 I have got my data in following format.. { "_id" : ObjectId("534fd4662d22a05415000000"), "product_id" : "50862224", "ean" : "8808992479390", "brand" : "LG", "model" : "37LH3000", "features" : [{ { "key" : "Screen Format", "value" : "16:9", }, { "key" : "DVD Player / Recorder", "value" : "No", }, "key" : "Weight in kg", "value" : "12.6", } ... so on ] } I need to compare features of one product with others and divide the result into separate categories ( 100% match, 50-99 % match) based on %

Sorting MongoDB GeoNear results by something other than distance?

流过昼夜 提交于 2019-12-01 06:30:41
I'm developing a PHP application in which we need to retrieve results within a certain boundary, but ordered by the create date of the results, not the distance. I figured MongoDB's geoNear command would be great for this, since it takes care of calculating the distance for each result. However, I was wondering if there was a way to specify sorting by the create_date attribute, rather than distance. Ideally I would create a compound key index of coordinates and create date, and then quickly retrieve all results in a specific area ordered by create date. Is this possible, or must I do my

What can be done with Mongo Aggregation / Performance of Mongo Aggregation

安稳与你 提交于 2019-12-01 04:08:51
I built a MongoDB. I want to do aggregation by certain grouping. I found this document , which will do that for me. Everything is ok, but certain limitations are pointed out: Output from the pipeline can only contain 16 megabytes. If your result set exceeds this limit, the aggregate command produces an error. If any single aggregation operation consumes more than 10 percent of system RAM the operation will produce an error. The aggregation system currently stores $group operations in memory, which may cause problems when processing a larger number of groups. How many rows / documents can I

Using Mongodb ObjectID as a document ID?

僤鯓⒐⒋嵵緔 提交于 2019-12-01 03:53:19
I'm trying to make a board with mongoDB. I want to assign document ID with ObjectID. If a user can access to a document page by http://www.example.com/4easdf123123 where "4easdf123123" is a mongoDB ObjectID. Is there any possible security threat, If I use and show mongo ObjectID in URL and using it as a document id? And any suggestion with assigning document ID with mongoDB? That doesn't look like a MongoDB ObjectID -- an ObjectID is 12 bytes of binary data, and when rendered as a hexadecimal string (the usual way to use it in a URL) it would be 24 characters long. I assume you're using the

What can be done with Mongo Aggregation / Performance of Mongo Aggregation

亡梦爱人 提交于 2019-12-01 01:49:01
问题 I built a MongoDB. I want to do aggregation by certain grouping. I found this document, which will do that for me. Everything is ok, but certain limitations are pointed out: Output from the pipeline can only contain 16 megabytes. If your result set exceeds this limit, the aggregate command produces an error. If any single aggregation operation consumes more than 10 percent of system RAM the operation will produce an error. The aggregation system currently stores $group operations in memory,

Query With Projection In MongoDB, PHP Syntax?

自作多情 提交于 2019-11-30 15:51:59
问题 What is the php syntax which will do the work which the following mongodb Shell does? > db.SoManySins.find({},{"_id":0,"FactoryCapacity":1}) 回答1: The MongoDB PHP driver functions are named similar to their shell counterparts, so in this case you would be using MongoCollection::find(). The PHP driver uses associative arrays to map fields to MongoDB queries. Since the PHP MongoCollection::find() documentation page doesn't currently include an example with projection, I've added one below for

Query With Projection In MongoDB, PHP Syntax?

ε祈祈猫儿з 提交于 2019-11-30 15:23:56
What is the php syntax which will do the work which the following mongodb Shell does? > db.SoManySins.find({},{"_id":0,"FactoryCapacity":1}) The MongoDB PHP driver functions are named similar to their shell counterparts, so in this case you would be using MongoCollection::find() . The PHP driver uses associative arrays to map fields to MongoDB queries. Since the PHP MongoCollection::find() documentation page doesn't currently include an example with projection, I've added one below for completeness: <?php $m = new MongoClient(); $db = $m->selectDB('test'); $collection = new MongoCollection($db

MongoDB Aggregation PHP, Group by Hours

安稳与你 提交于 2019-11-30 10:36:25
I have documents with the following format { "_id" : ObjectId("12e123123123123123"), "client_id" : "12345667889", "resource" : "test/test", "version" : "v2", "ts" : new Date("Wed, 02 Jan 2013 15:34:58 GMT +08:00") } The ts is a MongoDate() field. I am trying to use MongoDB aggregate function in php to group by client_id and hour and count usage to display in a table/graph This is my current attempt $usage = $this->db->Usage->aggregate(array(array( '$project' => array( 'hour' => array( 'years' => array( '$year' => '$ts' ), 'months' => array( '$month' => '$ts' ), 'days' => array( '$dayOfMonth' =

Mongodb php get id of new document?

故事扮演 提交于 2019-11-30 05:40:18
Creating a document: $db->collection->insert($content); // $newDocID = ??? I'm trying to get the new document's id. How? Thanks. According to the docs the array you pass to insert will be amended with an _id field: $db->collection->insert($content); $newDocID = $content['_id']; You can also get _id before insert. Just add _id field to document with new MongoId ie. $content['_id'] = new MongoId(); $db->collection->insert($content); Also there are nice benefits of this: You don't need fsync flag like posted in comment by ZagNut in previous answer. So you don't need to wait for reply from DB. You