I have been trying to fix this problem since days and I can't really solve it.
I'm trying to use mongo DB for my first time and here's my problem:
$id = utf8_encode($_POST['mongo']);
$query=array("id" => $id);
$conn = new Mongo("mongodb://localhost:27017");
$database = $conn->test;
$collection = $database->pages;
$doc = $collection->findOne($query);
The $id variable is set to 2, but findOne doesn't return anything.
If I try for example to change the id value in array with 2 [$query=array("id" => 2);] the DB returns the document that I need.
It's a mystery ahah.
Can anyone see an error?
Thanks
L
After connect you should be select your document
$id = utf8_encode($_POST['mongo']);
$query=array("id" => $id);
$m = new MongoClient('mongodb://localhost:27017');
$db = $m->selectDB('yourdocumentname');
$collection = new MongoCollection($db, 'yourcollectionname');
$doc = $collection->findOne($query);
var_dump($doc);
Related
I'm a beginner in PHP. My PHP version 7.3 and I cannot use MongoClient class because I have an error. So How can I get the size of the collection MongoDB/Drive class in Php?
$m =new MongoDB\Driver\Manager("mongodb://localhost:27017");
$query = new MongoDB\Driver\Query([]);
$res = $m->executeQuery("Paper.Papers", $query);
print_r(count(iterator_to_array($res)));
My solution is inefficient. Is there a different way?
UPDATE
I found alternative way to get size of collection:
$command = new MongoDB\Driver\Command(["dbstats" => 1]);
$res = $m->executeCommand("Paper", $command);
$stats = current($res->toArray());
Then;
print_r($stats->objects);
Output : 6000
I am trying to display the count of items based on a category for a specific user email.
This is my PHP code for the following
$email = $_SESSION['loggedInUserEmail'];
//Connect to MongoDB
$mongoClient = new MongoClient();
//Select a database
$db = $mongoClient->finchest;
$CheckEmail = [
"CustEmail" => $email,
];
$collection = $db->History;
$Criteria = array("Category" => "Fish Skins");
$returnVal = $collection->find($CheckEmail)->count($Criteria);
echo $returnVal;
This code gives me the following error
Warning: MongoCursor::count() expects parameter 1 to be boolean, array given in C:\www\FinChest\PHP\recommend.php on line 89
Please help me solve it, THANKS A TON.
I have a collection with entries like:
"Name":"test",
"Description":"some desc here",
"Teams":[0:"idhash1",1:"idhash2"],
"clientId":"clienthash"
from which I return all items like this:
$filter = array('clientId' => $clientID);
$options = [];
$query = new MongoDB\Driver\Query($filter, $options);
$cursor = $this->conn->executeQuery('dbname.collectionname', $query);
Now I want to add another filter on team value:
$filter = array('clientId' => $clientID,'Teams'=>'idhash1');
$options = [];
$query = new MongoDB\Driver\Query($filter, $options);
$cursor = $this->conn->executeQuery('dbname.collectionname', $query);
which obviously doesn't work. How would I get this to work? I am on PHP7.0, MongoDB 4.0 and extension version 1.4.2
This would be helpful
$insert in MongoDB with Php
http://php.net/manual/en/mongocollection.insert.php
What I decided to do was add the teamid's not only in an array but also as separate keys:
"Name":"test",
"Description":"some desc here",
"Teams":[0:"idhash1",1:"idhash2"],
"clientId":"clienthash",
"Team_idhash1":1,
"Team_idhash2":1
Making querying them very easy and it performs a lot better this way filtering on a team.
I'm using the new MongoDB PHP driver. I've been searching how I can get more than 100 records from a query.
I'm using executeCommand to pass the query. I think that in the old driver, the cursor object had a getNext method to get other pages, but It does not have anymore. How can I get the other "pages" from my query?
Simply use MongoDB\Driver\Query and MongoDB\Driver\Manager::executeQuery methods.
Here's a short sample for demonstration:
$manager = new MongoDB\Driver\Manager('mongodb://localhost:27017');
$query = new MongoDB\Driver\Query([], []);
$cursor = $manager->executeQuery('DB.Collection', $query);
$array = $cursor->toArray();
Note that resulting $array contains documents (records) as instances of stdClass Object.
I tried this solution and it returns all the documents in collection.
<?php
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
$command = new MongoDB\Driver\Command(array('find' => "testColl"));
$cursor = $manager->executeCommand('testDb', $command);
print_r($cursor->toArray());
?>
How to use Extend to update the field?
Example SQL: UPDATE Empresa SET categorias = 'cat_01' WHERE id_user = 'dlkj83d'
$query = new ParseQuery('Empresa');
$query->equalTo("users", Session::get('user'));
$query->add("categorias", 'cat_01'); <- ERROR
$query->save();
Test
$query = new ParseQuery('Empresa');
$query->equalTo("users", Session::get('user'));
$empresa = new ParseObject($query);
$empresa->add('categorias', array('cat_01'));
$empresa->save();
Does not work.
:(
There is no such ParseQuery::add() function. You are trying to cobble an UPDATE into a SELECT... you have to first find() the resulting ParseObjects and iterate through each one:
$query = new ParseQuery('Empresa');
$query->equalTo('users', Session::get('user'));
$results = $query->find(); // Returns an array of ParseObjects
foreach ($results as $res) {
$res->add('categorias', 'cat_01');
$res->save(); // Make sure you write Exception handling for this in the future
}
There is also a ParseObject::set and setArray function that will achieve the same thing, as add may be getting deprecated.
Perfect.
$query = new ParseQuery('Restaurante');
$query->equalTo("users", Session::get('user'));
$results = $query->first();
$results->add('categorias', array('cat_02'));
$results->save();
I still have some difficulties regarding the method NoSQL. Knowing that the reasoning is different.
Thank you for your attention.
You are part of the development team?