mongodb-php

how to ignore duplicate documents when using insertMany in mongodb php library?

二次信任 提交于 2019-12-06 18:43:32
问题 I am using mongo php library, and trying to insert some old data into mongodb. I used insertMany() method and pass a huge array of document, that may have duplicate documents on unique indexes. Lets say I have a users collection and have these indexes: [ { "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "test.users" }, { "v" : 1, "unique" : true, "key" : { "email" : 1 }, "name" : "shop_id_1_title_1", "ns" : "test.users" } ] If there's a duplicate document, MongoDB\Driver\Exception

MongoDB Aggregate Variable to create Multiple Fields?

孤街浪徒 提交于 2019-12-06 15:03:26
Given the following query, what is the best method to use $$priceToInflationRatio to help create multiple calculated fields? From what I have read on $let, it appears to only work for creating a single field -- I would like to use the variables across my entire $project section. Is that possible? db.books.aggregate([ {$project: { 'priceInflationresult': { $let: { vars: { 'priceToInflationRatio': { $multiply: [{'$divide': [{'$subtract': ['$price', 1]}, 5]}, 10] } }, in: { '$cond': [ {'$gt': ['$price', 5]}, {'$mod': ['$$priceToInflationRatio', 1]}, 1 ] }, } } }} ]) The in part of a $let

How can I use MongoDB in Kohana?

假如想象 提交于 2019-12-06 09:34:58
Please, give me some startup guidance on how to use MongoDB in Kohana[v3.2]. I downloaded the Kohana Module from Mongo Module for kohana and have set up the MongoDB driver for PHP. Now I'm trying to create a new database and to save a record into it. You Don't actually need the MangoDB module for Kohana that you linked to. You can just use PHP's MongoDB Native Driver. The mongo query language is pretty simple, if you know some MySQL then this page would help... http://www.php.net/manual/en/mongo.sqltomongo.php Here is an example using the driver, it connects, starts and inserts into a new

mongodb php getting fields unique values

只谈情不闲聊 提交于 2019-12-06 07:24:07
问题 I'm trying to get a list of unique values from the 'type' field from my mongodb collection. Example documents below: { "_id" : ..., "type" : "report", "tasks" : ... } { "_id" : ..., "type" : "research", "tasks" : ... } { "_id" : ..., "type" : "memo", "tasks" : ... } { "_id" : ..., "type" : "memo", "tasks" : ... } { "_id" : ..., "type" : "report", "tasks" : ... } { "_id" : ..., "type" : "report", "tasks" : ... } I'm looking for, ordered by frequency, the unique types that are in the type field

Calling a stored procedure via PHP in MongoDB

自古美人都是妖i 提交于 2019-12-06 07:14:20
Dear everybody who can help, I have this PHP > MongoDB problem, I want to call a stored procedure (stored in db.system.js collection) via PHP. I have no parameters, only a returning JSON object which looks like this: {"archived":[the number of the archived messages]} It works good in the shell on the database server, but when I try to call it via the PHP driver it just doesn't "say" anything... My code looks like this: $DB->execute( new MongoCode( "function () { return archiveMessages(); }" ) ); I have also tried to use somehow like this: $DB->execute("archiveMessages()"); Please help, I've

PHP: Is it bad design to serialize objects and stick them in the database for later?

℡╲_俬逩灬. 提交于 2019-12-06 05:17:54
问题 I am planning and researching my switch from MySQL to MongoDB right now and I just had an interesting thought... I have a bunch of hierarchical objects that I need to store in the database. My current method is to have a bunch of embedded documents in a collection. They will never need to be searched for. Would it possibly make sense just to serialize the PHP objects, stick them in the DB, and then unserialize them back into PHP objects when I want to use them? The alternative is using

unable to save PHP objects in mongodb

允我心安 提交于 2019-12-06 03:45:45
Getting 'zero-length keys are not allowed' FATAL error while saving object. Is it due to presence of __construct() ? I believe PHP object is allowed in save() method. class Address{ private $name; private $company; private $zip; public function __construct($name,$company,$zip){ $this->name = $name; $this->company = $company; $this->zip = $zip; } } $newAddress = new Address("james","google",678); print_r($newAddress); // Address Object ( [name:Address:private] => james [company:Address:private] => // google [zip:Address:private] => 678 ) $addresses->save($newAddress); Fatal error: Uncaught

What happens when connections to MongoDB are not closed?

冷暖自知 提交于 2019-12-06 02:23:41
问题 I have the following PHP script that basically connects to the MongoDB, writes a document and then closes the connection 19000 times: <?php for($i=0; $i < 19000; $i++) { $con = new Mongo("mongodb://localhost:27017"); $db = $con->selectDB('test'); $col = $db->selectCollection('close_wait_test'); $col->insert(array('Test' => 'Value1')); $con->close(); } ?> Running this script once works fine, but if I run the script a few seconds later, I get the 'Cannot assign requested address' exception,

how to integrate mongodb library in codeigniter for database connection with mongodb

匆匆过客 提交于 2019-12-05 11:40:47
I am creating application with codeigniter and mongodb database. As codeigniter doesn't contain mongodbs driver, I have used built in library to connect with mongodb. I have used this link to create mongodb configration file and Mongo_db library. The configration file is like application\config\mongodb: <?php $config['default']['mongo_hostbase'] = 'localhost:27017'; $config['default']['mongo_database'] = 'test'; $config['default']['mongo_username'] = ''; $config['default']['mongo_password'] = ''; $config['default']['mongo_persist'] = FALSE; $config['default']['mongo_persist_key'] = 'ci_persist

How to query mongo in php?

不问归期 提交于 2019-12-05 09:53:24
So I've got this simple login function that is trying to match email address with a password in the database and compare it with the user entered data via form. function login($email, $password){ $m = new Mongo("localhost"); $m->connect(); $db = $m->users; $collection = $db->test_collection; echo "<pre>"; var_dump($collection->findOne(array('name' => 'john'))); //returns correctly var_dump($collection->find(array('name' => 'john'))); //returns mongo cursor object echo "</pre>"; } I don't understand why the find() only returns a cursor object. Answers? This is the mongo document array(5) { ["