How to update the table using array of values in Laravel 8 - eloquent

I do have a two array's, I would like to update the Db based on that.
Array ( [0] => Array ( [name] => 1 ) [1] => Array ( [name] => 1 ) )//Id array
Array ( [0] => Array ( [quantity] => 740 ) [1] => Array ( [quantity] => 705 ) ) //The values that I need to update based on the previous array.
Some thing like this,
DB::table('stock')->where('id' , $id_array)->update($value_array);
How Can I achieve that?

$id_array = array(
['name' => 1],
['name' => 2]
);
$value_array = array(
['quantity' => 740],
['quantity' => 705]
);
foreach($id_array as $key => $value) {
DB::table('stock')->where('id' , $id_array[$key]['name'])->update( $value_array[$key]['quantity']);
}
Assuming both arrays are equal size, we can loop through the $id_array and use the key index value for the $value_array.

Related

Group by in doctrine mongodb

I'm trying to make a group by in mongodb with doctrine.
I have made sentence with createQuery (doctrine) but not with MongoDB.
$qb = $this->_em->createQuery("Select sum(p.cantidad) FROM Application\Entity\ConjuntoProductos p where p.cesta=:cesta group by p.cesta");
$qb->setParameter('cesta', $idCesta);
$results=$qb->getResult();
I need to do in MongoDB.
Thank You.
The solution:
$results = $qb
->group ( array (
'id' => 1
), array (
'total' => 0
) )->reduce ( 'function ( curr, result ) { result.total += curr.cantidad;}' )
->field ( 'cesta.id' )->equals ( $idCesta )
->getQuery ()->execute ();

Creating an index on a nested field

I have a MongoDB document that is structured similar to the structure below follows. There are is a large number of these documents with thousands of people in them, so I want to speed things up by putting indexes on the people.#.search_columns.surname and people.#.search_columns.givenname. Howe can I do this in MongoDB? Thanks for your help.
[_id] => MongoId Object (
[$id] => 53b1b1ab72f4f852140dbdc9
)
[name] => People From 1921
[people] => Array (
[0] => Array (
[name] => Barada, Valentine
[search_columns] => Array (
[surname] => Array (
[0] => Mardan,
[1] => Barada
)
[givenname] => Array (
[0] => Valentine
)
)
)
[1] => Array (
[name] => Barsaloux, Nicholas
[search_columns] => Array (
[surname] => Array (
[1] => Barsaloux
)
[givenname] => Array (
[0] => Nicholas
)
[place] => Array (
)
)
)
)
You can create indexes for nested fields using the dot notation:
db.collection.ensureIndex({'people.search_columns.surname': 1});
db.collection.ensureIndex({'people.search_columns.givenname': 1});

Searching Record in mongodb using php

I am getting the records from collections in following format using find() function of mongodb
Array
(
[_id] => MongoId Object
(
[$id] => 52a9b86578e9288a378b4567
)
[0] => Array
(
[senderID] => 1
[followingID] => 15
[type] => TW
)
[1] => Array
(
[senderID] => 1
[followingID] => 16
[type] => TW
)
[2] => Array
(
[senderID] => 1
[followingID] => 17
[type] => TW
)
[3] => Array
(
[senderID] => 2
[followingID] => 17
[type] => TW
)
)
Now I want all the records whose senderID is 2. I have tried by passing array in find function
$myarray("senderID"=> 1)
$collection->find($myarray)
but it will not get the correct record.

Getting last row in mongodb

I am using find() function in mongodb and got a record in following format
Array
(
[_id] => MongoId Object
(
[$id] => 52a561ea78e9288b568b4567
)
[friendID] => 1
[name] => Shobhit Srivastav
[senderID] => 2
[receiverID] => 1386570218
[receiverType] => TW
[receiverUserID] => 3
[status] => 0
)
Array
(
[_id] => MongoId Object
(
[$id] => 52a5623178e928d8568b4567
)
[friendID] => 2
[name] => Sachin Tendulkar
[senderID] => 2
[receiverID] => 1386570289
[receiverType] => TW
[receiverUserID] => 3
[status] => 0
)
but I want record of last row which are inserted in the table. how can I find??
Thanks in advance!!
If you want to get last record inserted in the table, then sort by ObjectId in descending order:
sorting on an _id field that stores ObjectId values is roughly
equivalent to sorting by creation time.
and get first record:
db.collection.find().sort( { _id : -1 } ).limit(1);
With php driver it will look like
$doc = $collection->find()->sort(array("_id" => -1))->limit(1)->getNext();

How can I find a bounding box that encapsulates a specific point?

[uppercaseName] => ATLANTA, GA
[description] => Atlanta, GA
[name] => Atlanta, GA
[_id] => MongoId Object (
)
[addedOn] => MongoDate Object (
[sec] => 1318879015
[usec] => 517000
)
[excludePoints] => Array (
)
[boundingBox] => Array (
[0] => Array (
[lon] => -84.516
[lat] => 33.6747
)
[1] => Array (
[lon] => -84.516
[lat] => 33.8232
)
[2] => Array (
[lon] => -84.2599
[lat] => 33.8232
)
[3] => Array (
[lon] => -84.2599
[lat] => 33.6747
)
)
That's my document (in MongoDB). I have several such documents and I want to run a query to find all documents that have a bounding box that encapsulates that specific Long and Lat. How would I do this?
Unfortunately, this is not currently possible with MongoDB. MongoDB can index points and find all of the documents inside an area, but cannot index areas and query all of the documents containing an area that encloses a gives point.
There is a feature request for this: https://jira.mongodb.org/browse/SERVER-2874
Presently there is no scheduled date for this feature. Please vote for it!
For more information on Geospatial Indexing and what it is capable of, please see the MongoDB documentation:
http://www.mongodb.org/display/DOCS/Geospatial+Indexing