Query the nested documents inside mongodb arrays - mongodb

When updating Mongo db I want to update a document inside multiple nested arrays. But none of my querys worked when tried to update it. Below is the of existing table:
Array
(
[0] => Array
(
[_id] => MongoId Object
(
[$id] => 5ce268ef6803fa8f237b23c6
)
[member_number] => 26b091bdd3f9505b2103a187f3ac6643
[member_forename] => 1d40521662cc484d7c06992c61303ba6
[member_surname] => 581c78bf6fe11cc0ff53a805a100d441
[certificates] => Array
(
[0] => Array
(
[member_id] => MongoId Object
(
[$id] => 5ce268ef6803fa8f237b23c6
)
[certificate_id] => MongoId Object
(
[$id] => 5ce4dd3d6803fa15454cbed5
)
[skills_completed] => N
[certificate_issued] => N
[details] => Array
(
[0] => Array
(
[member_id] => MongoId Object
(
[$id] => 5ce268ef6803fa8f237b23c6
)
[session_id] => MongoId Object
(
[$id] => 5ce4f1a96803fa574e4cbede
)
[mark_status] => A
[cd_status] => A
[gradeId] => MongoId Object
(
[$id] => 5ca48ccfa36f74db9a8a0620
)
[gradeKey] => NP
[achieved_date] => MongoDate Object
(
[sec] => 1558531565
[usec] => 713000
)
[detail_id] => MongoId Object
(
[$id] => 5ce4dd526803fa1f454cbed7
)
[marked_by] => MongoId Object
(
[$id] => 5cb5622a6803fa640b7b23c7
)
)
)
[certificate_issued_by] =>
[certificate_session_id] =>
[sc_date] =>
)
[updated_at] => MongoDate Object
(
[sec] => 1558341930
[usec] => 482000
)
)
)
I want to update one of the details section that is nested inside certificates.the certificate array is inside member table.
my matching condition is _id = member id ,
certificates.certificate_id=xxxx,
certificates.details.detail_id=xxxx
I have the full array that will replace zeroth position of details array.how to do this?

Assuming there are embedded associations involved, letting Mongoid do the work should be straightforward:
class Foo
include Mongoid::Document
embeds_many :bars
end
class Bar
include Mongoid::Document
has_one :thing
embedded_in :foo
end
class Thing
include Mongoid::Document
belongs_to :bar
end
foo = Foo.new
foo.bars = [Bar.new, Bar.new]
foo.save!
foo.bars[0].thing = Thing.new
foo.save!
foo.reload
foo.bars.map(&:thing)
# => [#<Thing _id: 5ce85f47026d7c20c4c0613a, bar_id: BSON::ObjectId('5ce85f47026d7c20c4c06138')>, nil]
Take note of https://jira.mongodb.org/browse/MONGOID-2951 - in the above example I changed a field of one of the Bar instances but I did not modify the list of Bars.

Related

Issue with first() attribute in Laravel 8

I tried to fetch the first element of my DB. When I wanted to bring it with first(), it showed me lots of data, and I couldn't access the data attributes.
Here is my simple Code .
Model Code:
$all_value_bulk=BulkEmail::where('status','processing')->where('processing_status',null)->first();
print_r($all_value_bulk);
Output::
App\Models\BulkEmail Object ( [table:protected] => bulk_email_verification [fillable:protected] => Array ( [0] => file_path [1] => user [2] => file_id [3] => status [4] => record [5] => unverified [6] => file_name ) [connection:protected] => mysql [primaryKey:protected] => id [keyType:protected] => int [incrementing] => 1 [with:protected] => Array ( ) [withCount:protected] => Array ( ) [preventsLazyLoading] => [perPage:protected] => 15 [exists] => 1 [wasRecentlyCreated] => [escapeWhenCastingToString:protected] => [attributes:protected] => Array ( [id] => 67 [file_path] => public/uploads/1654107283-test.csv [user] => 1 [file_id] => 74478 [record] => 6 [unverified] => 6 [created_at] => 2022-06-01 18:14:43 [updated_at] => 2022-06-01 19:16:29 [status] => processing [initial_invalid] => [file_ids] => [valid] => 2 [unknown] => 0 [invalid] => 2 [download_path] => https://client.myemailverifier.com/downloadreport/csv/74478 [download_status] => finished [ready_for_download] => 1 [finished_time] => 1654110989 [duplicate] => 1 [free_domain] => 4 [catch] => 1 [xls_file_path] => https://client.myemailverifier.com/downloadreport/xls/74478 [file_name] => test.csv [accept_all] => 0 [role_based] => 4 [processing_status] => ) [original:protected] => Array ( [id] => 67 [file_path] => public/uploads/1654107283-test.csv [user] => 1 [file_id] => 74478 [record] => 6 [unverified] => 6 [created_at] => 2022-06-01 18:14:43 [updated_at] => 2022-06-01 19:16:29 [status] => processing [initial_invalid] => [file_ids] => [valid] => 2 [unknown] => 0 [invalid] => 2 [download_path] => https://client.myemailverifier.com/downloadreport/csv/74478 [download_status] => finished [ready_for_download] => 1 [finished_time] => 1654110989 [duplicate] => 1 [free_domain] => 4 [catch] => 1 [xls_file_path] => https://client.myemailverifier.com/downloadreport/xls/74478 [file_name] => test.csv [accept_all] => 0 [role_based] => 4 [processing_status] => ) [changes:protected] => Array ( ) [casts:protected] => Array ( ) [classCastCache:protected] => Array ( ) [attributeCastCache:protected] => Array ( ) [dates:protected] => Array ( ) [dateFormat:protected] => [appends:protected] => Array ( ) [dispatchesEvents:protected] => Array ( ) [observables:protected] => Array ( ) [relations:protected] => Array ( ) [touches:protected] => Array ( ) [timestamps] => 1 [hidden:protected] => Array ( ) [visible:protected] => Array ( ) [guarded:protected] => Array ( [0] => * ) )
You can use dd() than you show first element
$all_value_bulk=BulkEmail::where('status','processing')->where('processing_status',null)->first();
dd($all_value_bulk);
Otherwise you use var_dump result Not easily readable, is it? The trick here is to View Source in browser, in case of Chrome/Windows it’s CTRL+U:
It returns array in object
You can try
$id_val = $all_value_bulk[0]->id;
This worked for me very well

write mongodb driver query with php fetch records from embedded array

Filter item from records array it should only return two items because records.score = 100 having only two items instead this it return me all the records. Can you please help on this thanks.
I have multiple records wherein I would like to fetch only filter records. where I am doing mistake please guide and suggest.
stdClass Object
(
[_id] => e5s65d5e5s65d5s65d44f12
[records] => Array
(
[0] => stdClass Object
(
[date] => MongoDB\BSON\UTCDateTime Object
(
[milliseconds] => 1609923848000
)
[score] => 100
[country] => US
[state] => Connecticut
[city] => Berlin
)
[1] => stdClass Object
(
[date] => MongoDB\BSON\UTCDateTime Object
(
[milliseconds] => 1609923501000
)
[score] => 100
[country] => US
[state] => California
[city] => Barstow
)
[2] => stdClass Object
(
[date] => MongoDB\BSON\UTCDateTime Object
(
[milliseconds] => 1609923157000
)
[score] => 145
[country] => US
[state] => Alabama
[city] => Alexander City
)
[3] => stdClass Object
(
[date] => MongoDB\BSON\UTCDateTime Object
(
[milliseconds] => 1609923108000
)
[score] => 150
[country] => US
[state] => Alaska
[city] => Anchorage
)
)
)
$mng = new MongoDB\Driver\Manager("mongoatlas/");
$filter = ['records.score' => '100'];
$query = new MongoDB\Driver\Query($filter, ['sort' => ['records.date' => 1], 'limit' => 6]);
$rows = $mng->executeQuery("db.table", $query);
expected result should be only two item whose state is Connecticut, California because their score are 100
Try this one
$command = new MongoDB\Driver\Command([
'aggregate' => 'collection',
'pipeline' => [
['$unwind' => '$records'],
['$match' => ['records.score' => '100']],
['$sort' => ['records.date' => 1]],
['$limit' => 6]
],
'cursor' => new stdClass,
]);
$cursor = $mng->executeCommand('database', $command);

Dot notation in MongoDB InsertOne

While performing InsertOne in MongoDB (4.0.5) from PHP I get the following exception:
[0] Detected unsupported PHP type for field path "context.trace.1.args.2": 7
According to the MongoDB documentation one is allowed to use dot notation in update commands. The doc however talks about this fact with $set as context.
It seems to me that a simple Update does not allow for dot notation. (just guessing)
Can someone explain the nuance/edge cases here?
===== follow up on request:
The document is created by Monolog.
And the document used in InsertOne looks like the following:
Array
(
[message] => file_get_contents( ...obfuscated... ): failed to open stream: Connection timed out
[context] => Array
(
[code] => 2
[file] => ...obfuscated...
[line] => 127
[trace] => Array
(
[0] => Array
(
[function] => handleError
[class] => Whoops\Run
[type] => ->
...obfuscated...
)
[1] => Array
(
[function] => file_get_contents
[args] => Array
(
[0] => https:// ...obfuscated...
[1] =>
[2] => 0
)
)
[2] => Array
(
[file] => ...obfuscated... .php
[line] => 127
[function] => file_get_contents
[args] => Array
(
[0] => https:// ...obfuscated...
)
)
[3] => Array
(
[file] => ...obfuscated... .php
[line] => 91
[args] => Array
(
[0] => ...obfuscated... .php
)
[function] => require_once
)
)
[notThrown] => 1
[type] => Whoops\Exception\ErrorException
)
[level] => 500
[level_name] => CRITICAL
[channel] => ...obfuscated...
[datetime] => MongoDB\BSON\UTCDateTime Object
(
[milliseconds] => 1551951362000
)
[extra] => Array
(
[cli] => Array
(
[user] => ...obfuscated...
[script] => ...obfuscated... .php
[argv] => Array
(
[0] => ...obfuscated...
)
)
[server_name] => ...obfuscated...
[server_ip] => ...obfuscated...
[process_id] => 3273
[memory_peak_usage] => 5.75 MB
[memory_usage] => 5.75 MB
[git] => Array
(
[ ...obfuscated... ] => Array
(
[branch] => ...obfuscated...
[commit] => ...obfuscated...
)
[publisher] => Array
(
[branch] => ...obfuscated...
[commit] => ...obfuscated...
)
)
)
[server_name] => ...obfuscated...
[_id] => MongoDB\BSON\ObjectId Object
(
[oid] => 5c81e6029caba40cc96bd831
)
)
I suspect Neil Lunn already answers the question.
But now I wonder whether this is a shortcoming of Monolog or not. And what approach to take to solve this issue.

Handle post attachments Facebook API SDK 4

Friend, I can not handle the data that return the call. How can I interact with the data, specifically with attachments. the complete result of calling this the link: http://pastebin.com/fLjV0Zu2
I'm using the conversion method for array, but it seems that this method does not interact at all levels of the object. So I can not reach the data attachments to handle.
$graphObject = $response->getGraphObject()->asArray();
$media = $graphObject->getProperty('attachments')->asArray();
print_r($media);
Result:
Array
(
[0] => stdClass Object
(
[subattachments] => stdClass Object
(
[data] => Array
(
[0] => stdClass Object
(
[media] => stdClass Object
(
[image] => stdClass Object
(
[height] => 483
[src] => https://scontent-a.xx.fbcdn.net/hphotos-xpa1/v/t1.0-9/s720x720/10891987_811524088885970_7055896286836804482_n.jpg?oh=895cce48d5da3e59b374fad2f7ec8f69&oe=55297847
[width] => 720
)
)
[target] => stdClass Object
(
[id] => 811524088885970
[url] => https://www.facebook.com/photo.php?fbid=811524088885970&set=gm.767232516689804&type=1
)
[type] => photo
[url] => https://www.facebook.com/photo.php?fbid=811524088885970&set=gm.767232516689804&type=1
)
[1] => stdClass Object
(
[media] => stdClass Object
(
[image] => stdClass Object
(
[height] => 404
[src] => https://scontent-a.xx.fbcdn.net/hphotos-xaf1/v/t1.0-9/s720x720/10885099_811524235552622_234704175575422999_n.jpg?oh=d94c5f69852665adb5a8bae2217cc900&oe=552A30F4
[width] => 720
)
)
[target] => stdClass Object
(
[id] => 811524235552622
[url] => https://www.facebook.com/photo.php?fbid=811524235552622&set=gm.767232516689804&type=1
)
[type] => photo
[url] => https://www.facebook.com/photo.php?fbid=811524235552622&set=gm.767232516689804&type=1
)
[2] => stdClass Object
(
[media] => stdClass Object
(
[image] => stdClass Object
(
[height] => 404
[src] => https://scontent-b.xx.fbcdn.net/hphotos-xap1/v/t1.0-9/s720x720/10898031_811524285552617_7673546158326216312_n.jpg?oh=3376e9140822a7a79904f58f30214c5d&oe=552D6772
[width] => 720
)
)
[target] => stdClass Object
(
[id] => 811524285552617
[url] => https://www.facebook.com/photo.php?fbid=811524285552617&set=gm.767232516689804&type=1
)
[type] => photo
[url] => https://www.facebook.com/photo.php?fbid=811524285552617&set=gm.767232516689804&type=1
)
)
)
[target] => stdClass Object
(
[id] => 767232516689804
[url] => https://www.facebook.com/media/set/?set=pcb.767232516689804&type=1
)
[title] => Photos from Conceicao Fernandes's post
[type] => album
[url] => https://www.facebook.com/media/set/?set=pcb.767232516689804&type=1
)
)
For those who have the same problem. Or rather doubt.
The friend WizKid cleared my ideas.
Even using the correct method to convert the result of GraphObject using Facebook PHP SDK4, calling asArray(); to access the data, I used the form below with success.
$media = $graphObject->getProperty('attachments')->asArray();
$media[0]->subattachments->data;
To interact with the data I'm using foreach()
$data = $media[0]->subattachments->data;
foreach($data as $k => $v) {
//Here you interacted with the data
}

Zend_Db Enum Values

I find this solution
$metadata = $result->getTable()->info('metadata');
echo $metadata['Continent']['DATA_TYPE'];
Hi,
I want to get enum values in Zend_Db.
My Code:
$select = $this->select();
$result = $select->fetchAll();
print_r($result->getTable());
Output:
Example Object
(
[_name] => country
[query] => Zend_Db_Table_Select Object
(
[_info:protected] => Array
(
[schema] =>
[name] => country
[cols] => Array
(
[0] => Code
[1] => Continent
)
[primary] => Array
(
[1] => Code
)
[metadata] => Array
(
[Continent] => Array
(
[SCHEMA_NAME] =>
[TABLE_NAME] => country
[COLUMN_NAME] => Continent
[COLUMN_POSITION] => 3
[DATA_TYPE] => enum('Asia','Europe','North America','Africa','Oceania','Antarctica','South America')
[DEFAULT] => Asia
[NULLABLE] =>
[LENGTH] =>
[SCALE] =>
[PRECISION] =>
[UNSIGNED] =>
[PRIMARY] =>
[PRIMARY_POSITION] =>
[IDENTITY] =>
)
I see enum values in data_type but i don't get this values. How can get data_type?
I find this solution
$metadata = $result->getTable()->info('metadata');
echo $metadata['Continent']['DATA_TYPE'];