Getting last row in mongodb - 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();

Related

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

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.

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});

How to increase MongoDB performance

I am developing an application in MongoDB, CakePHP. I have 145,000 record in my database. When I query for records using following command then it takes 12seconds. Which is very bad for my application.
$params= array('aggregate'=>array(
array('$project'=>array('as'=>1,'pid'=>1,'st'=>1)),
array('$unwind'=>'$as'),
array('$match' => array('pd'=>array('$gt'=>$f,'$lt'=>$t),'pid'=>$project_id)),
array('$group'=>array('_id'=>'$as')),
array('$sort'=>array('_id'=>1)),
array('$limit'=>10)
)
);
$results = $this->Detail->find('all',array('conditions'=>$params));
Can anyone help me in reducing time for query.
I have indexes on as & pid. My system RAM is 1.5GB.
I got following data as result
[1] => Array
(
[Detail] => Array
(
[_id] => Array
(
[0] => "superfone" Llc,moscow,ru
)
)
)
[2] => Array
(
[Detail] => Array
(
[_id] => Array
(
[0] => "superphone" Llc,moscow,ru
)
)
)
Before performing the find, make sure to set the recursive level to the level you need, then report back on your performance.
$this->Detail->recursive = -1; // no joins
results = $this->Detail->find('all',array('conditions'=>$params));
or
$this->Detail->recursive = 0; // data + domain
results = $this->Detail->find('all',array('conditions'=>$params));
Not sure what level of recursive you need, so give them a test, but default is 1 and it can be a costly exercise.
ref
http://book.cakephp.org/2.0/en/models/model-attributes.html#recursive
The reference also says to make -1 your default, and raise the recursive levels as you need them, this can be done by adding the following to AppModel
public $recursive = -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.

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