intekhabrizvi / codeigniter-mongo-library Goto Github PK
View Code? Open in Web Editor NEWMongoDB Library for Codeigniter with advance features like aggregation, journal, write concern and query profiling
Home Page: http://intekhabrizvi.github.io
MongoDB Library for Codeigniter with advance features like aggregation, journal, write concern and query profiling
Home Page: http://intekhabrizvi.github.io
Which function can i use if i want to update a field inside an object ?
For Example:
{
"day": "2017-01-27",
"clicks": {
"50,100": 1,
"100,150": 3
}
}
This is a document..how can i increment the value of "50,100" key ?
Any kind of help is appreciated.
db.stores.find(
{ $text: { $search: "java coffee shop" } },
{ score: { $meta: "textScore" } }
).sort( { score: { $meta: "textScore" } } )
I am not able to find any function that can meet above query solution. Please help me.
i need to count of the affected document after update in mongo db , is there any option to find out
this library cannot run in new mongodb PHP extension
Fatal error: Call to a member function explain() on a non-object in app\libraries\Mongo_db.php on line 1763
HI @ALL,
i got the error
`An uncaught Exception was encountered
Type: MongoDB\Driver\Exception\CommandException
Message: The 'cursor' option is required, except for aggregate with the explain argument
Filename: C:\inetpub\wwwroot\fileshark\application\libraries\Mongo_db.php
Line Number: 1776`
I did found the same issues as solved here but it dossent help me.
I do not know if I am using the function correctly.
My Option
$opn = array(['$group' => ['accessRules' => array('$first' => array('Identity')), ]], ['cursor' => ['batchSize' => 0,]] );
If you load the PECL driver "mongodb" you get the error:
The MongoDB PECL extension has not been installed or enabled
The error is very deceptive because it really should say "The Mongo PECL" ext isn't found and not the "MongoDB" extension. This is because this git repo only works with the PECL library called "mongo". The "mongo" library is not only deprecated but has a PHP requirement of:
pecl/mongo requires PHP (version >= 5.3.0, version <= 5.99.99)
If it is true that the there is no way to use the current "mongodb" driver and we have to downgrade our PHP to 5.x for this to work, can this be added in big bold letters in the readme? I think a lot of us are wasting a lot of time with this library because instructions are unclear.
Also, it would be courteous to change this line:
show_error("The MongoDB PECL extension has not been installed or enabled", 500);
To say this:
show_error("The Mongo PECL extension has not been installed or enabled", 500);
don't know how to update a record where some record is equal to xxx
also, want to ask about upsert function, can this library work for upsert function?
how to get the last insert id ?
and I tried aggregate error like this appeared "Aggregation operation failed: localhost: 27017: The 'cursor' option is required, except for aggregate with the explain argument"
code
$res = $this->mongo_db->aggregate(
'player',
array(
'$lookup' => array(
'from' => 'player_detail',
'localField' => 'player_detail_id',
'foreignField' => '_id',
'as' => 'detail',
)
),
array('cursor' => array('batchSize' => 0))
);
Hello
Why do i cannot add index on collection?
The error is Index could not be created to MongoDB Collection because no keys were specified
My code is $this->mongo_db->add_index('omid','time');
When i use $this->mongo_db->add_index('omid',array('time' => "ASC"));
again have error Message: Undefined variable: command
Thank you guys
The MongoDB PECL extension has not been installed or enabled
If I use Composer mongo lib its working fine but getting issue on CI
something like this . ->like('aaa','bbb')->or_like('ccc','ddd') ..
i'm doest't found in your library . sorry
Hello,
I noticed that using the library native order_by function, the special characters order got wrong. The result was something like this:
But the correct order is:
So I've used the following solution for this (PHP 5.3+):
// Old database request
$myArray = $this->mongo_db->order_by(array('city' => 'asc'))->get('cities');
// New database request
$myArray = $this->mongo_db->get('cities');
// Remove order_by function and use this instead
usort($myArray, function($o, $n) {
$ot = iconv('UTF-8', 'ASCII//TRANSLIT', $o['city']);
$nt = iconv('UTF-8', 'ASCII//TRANSLIT', $n['city']);
return strcmp($ot, $nt);
});
Well, just sharing a solution for a bug that I believe should be fixed. Cheers!
Here is my mongo db query to find data , but unable to perform this from library
db.getCollection('employer').find({},{companies:{$slice:1}})
How To Use this library ???
we are trying to run below query. do we have $elemMatch support in this library. or can we create/build custom/native query which can be directly passed to mongodb.
var Query = {
userId: {
$ne: id
},
users: {
$elemMatch: {
$eq: id
}
}
};
how can i join two collection?
Check my collections bellow :
First collection, called books, having the following data:
{
"isbn": "12",
"title": "Some cool book",
"author": "John Doe"
}
{
"isbn": "13",
"title": "Another awesome book",
"author": "Jane Roe"
}
And the second collection, called books_selling_data, having the following data:
{
"_id": ObjectId("56e31bcf76cdf52e541d9d26"),
"isbn": "12",
"copies_sold": 12500
}
{
"_id": ObjectId("56e31ce076cdf52e541d9d28"),
"isbn": "13",
"copies_sold": 720050
}
All settings
$config['mongo_db']['live']['no_auth'] = FALSE;
$config['mongo_db']['live']['hostname'] = '-.-.-.-';
$config['mongo_db']['live']['port'] = '27017';
$config['mongo_db']['live']['username'] = '-----';
$config['mongo_db']['live']['password'] = '----';
$config['mongo_db']['live']['database'] = 'admin';
$config['mongo_db']['live']['db_debug'] = TRUE;
$config['mongo_db']['live']['return_as'] = 'array';
$config['mongo_db']['live']['write_concerns'] = (int)1;
$config['mongo_db']['live']['journal'] = TRUE;
$config['mongo_db']['live']['read_preference'] = NULL;
$config['mongo_db']['live']['read_preference_tags'] = NULL;
I got a problem when I use db.collection.aggregate in MongoDB.
I have a data structure like:
`$ops = array(
array(
'$group' => array(
"_id" => null,
"total" => array( '$sum' => '$aulas' )
)
)
);
$dados['consulta1'] = $this->mongo_db->aggregate('matriz_curricular', $ops);`
An Error Was Encountered
Aggregation operation failed: localhost:27017: The 'cursor' option is required, unless 'explain' is true
Add this funcion
function create_next_link($id) { $id = new MongoId($id); $this->mongo_db->where(array('status' => 1)); $this->mongo_db->where_gt('_id',$id); $this->mongo_db->order_by(array('_id'=>'DESC')); $this->mongo_db->limit(1); return $this->mongo_db->get('property'); }
Above code not returning correct document! Do you have any ideas ?
when i use ike query in return i got blank array
I write code with method push and i receive Error warning
Code:
$this->mongo_db->where(array('mid'=>'1349218578462396'))->push('data', array('message' =>'test123'))->update('message');
Error:
Update of data into MongoDB failed: localhost:27017: The field 'data' must be an array but is of type Object in document
Hi @intekhabrizvi I'm using your mongodb library but I have a problem, I get this error when I query on mongoDB. I would be very happy if you help
$ops = array(
array('$lookup'=> array('from'=>'balances', 'localField'=> '_id', 'foreignField'=>"OrderID", 'as'=> "balances"))
);
$orders = $this->mongo_db->aggregate('orders', $ops);
print_r($orders);
method like error, and MongoRegex is invalid in php7.
then,change
$regex = "/$value/$flags"; $this->wheres[$field] = new MongoRegex($regex);
to
$this->wheres[$field] = [ '$regex' => '.*'. $value . '.*', '$options' => $flags, ];
and it works well.
Hi there, this is an awesome library. But I think it would be much more awesome with mongos join methods. https://www.mongodb.com/blog/post/joins-and-other-aggregation-enhancements-coming-in-mongodb-3-2-part-1-of-3-introduction
Sample schema.
db.left.find()
{ "_id" : ObjectId("56b0ea87e64ac61fa9b36b2b"), "name" : "Rod", "ref" : 1 }
{ "_id" : ObjectId("56b0ea87e64ac61fa9b36b2c"), "name" : "Jane", "ref" : 1 }
{ "_id" : ObjectId("56b0ea87e64ac61fa9b36b2d"), "name" : "Freddy", "ref" : 2 }
db.right.find()
{ "_id" : 1, "rightData" : "One" }
{ "_id" : 2, "rightData" : "Two" }
db.left.aggregate(
[{
$lookup:
{
from: "right",
localField: "ref",
foreignField: "_id",
as: "stuffFromRight"
}
}])
{ "_id" : ObjectId("56b0ea87e64ac61fa9b36b2b"), "name" : "Rod", "ref" : 1, "stuffFromRight" : [ { "_id" : 1, "rightData" : "One" } ] }
{ "_id" : ObjectId("56b0ea87e64ac61fa9b36b2c"), "name" : "Jane", "ref" : 1, "stuffFromRight" : [ { "_id" : 1, "rightData" : "One" } ] }
{ "_id" : ObjectId("56b0ea87e64ac61fa9b36b2d"), "name" : "Freddy", "ref" : 2, "stuffFromRight" : [ { "_id" : 2, "rightData" : "Two" } ] }
I am using aggrigate function like:
->aggregate ('$concat: [ "FirstName", " - ", "LastName" ]')
I know this is stupid way, please guide me.
Hi,
Can we have findandmodify support in this lib.
http://php.net/manual/en/mongocollection.findandmodify.php
we need transactions in this great library. tlike sql server
$this->db->trans_begin();
$this->db->trans_commit();
$this->db->trans_rollback();
thank you
Just a suggestion..
$query
were use instead of $command
mapreduce
, this changes seem to work ok.- 1540: public function command($collection, $command = array())
+ 1540: public function command($collection = FALSE, $command = array())
- 1542: if (empty($collection))
- 1543: {
- 1544: show_error("No Mongo collection specified to run command", 500);
- 1545: }
- 1554: $returns = $this->db->{$collection}->command($query);
+ 1554: if (!empty($collection))
+ 0: {
+ 0: $returns = $this->db->{$collection}->command($command);
+ 0: }
+ 0: else
+ 0: {
+ 0: $returns = $this->db->command($command);
+ 0: }
How to find documents with ObjectID? When I use following data request:
$this->mongo_db->where(array('_id'=>'ObjectId("570e938b407729003100002a")'))->get('school');
It gives me null response, but data exist with that object id, I tried it on bash.
Getting "The MongoDB PECL extension has not been installed or enabled", despite phpinfo reporting the extension being installed:
mongodb version: 1.1.7
If I try to initialize MongoClient I get:
Fatal error: Class 'MongoClient' not found
But if I use the latest class syntax:
new MongoDB\Driver\Manager("mongodb://localhost:27017");
It works.
Is the library outdated?
I followed the process from this link
but got an error while running a test code
The MongoDB PECL extension has not been installed or enabled
class Test_mongo extends CI_Controller { public function getData(){ $result = $this->mongo_db->select('blood_request'); echo $result; } }
What could be the issue?
I am getting this error message while getting large records
Message: localhost:27017: aggregation result exceeds maximum document size (16MB) php
After that I have added allowDiskUse and maxTimeMS but its still same error
$collection->aggregate($pipeline, array("allowDiskUse" => true, "maxTimeMS" => 1800000));
Is left join is possible
how can i reach last query in mongodb ?
please add command function as follow:
/**
* Command.
*
* Runs a MongoDB command (such as GeoNear). See the MongoDB documentation for more usage scenarios - http://dochub.mongodb.org/co
re/commands
*
* <code>
* $this->mongo_db->command(array('geoNear'=>'buildings', 'near'=>array(53.228482, -0.547847), 'num' => 10, 'nearSphere'=>true));
* </code>
*
* @param array $query The command query
*
* @access public
* @return object
*/
public function command($query = array())
{
try
{
$execute = $this->db->command($query);
return $execute;
}
catch (MongoCursorException $exception)
{
show_error('MongoDB command failed to execute: ' . $exception->getMessage(), 500);
}
}
How can i Execute geoNear Query ?
Hi,
First of all, thanks for sharing this.
I think this is a bug;
The passed $options
was cancelled by defaulting values. It should be the other way around.
// Mongo_db->update()
1244: - $options = array_merge($options, array('w' => $this->write_concerns, 'j'=>$this->journal, 'multiple' => FALSE));
1244: +$options = array_merge(array('w' => $this->write_concerns, 'j'=>$this->journal, 'multiple' => FALSE), $options);
// Mongo_db->update_all()
1284: - $options = array_merge($options, array('w' => $this->write_concerns, 'j'=>$this->journal, 'multiple' => TRUE));
1284: + $options = array_merge(array('w' => $this->write_concerns, 'j'=>$this->journal, 'multiple' => TRUE), $options);
In current version PORT is required
. When it is necessary to be connected to /tmp/mongodb-27017.sock
if i typed PORT, i see PHP error that file /tmp/mongodb-27017.sock:27017
is undefined
.
Your dns-constructor:
$dns = "mongodb://{$this->hostname}:{$this->port}/{$this->database}";
You shall add additional check on empty of PORT. For this purpose it is necessary to change functions: prepare
& connect
.
Hi @intekhabrizvi,
I create this issue because I dont see any contact information from your profile.
I love and using your library in my projects. But I working on many kind of project such as ZF, Slim, etc ...
May you allow me to copy and convert your library to a standalone library so we can use it in any kinds of php project not just CodeIgniter?
My library will be hosted as a public repository in Github and have a copyright and link to your main repository.
Please give me your opinion.
Thank you!
try
{
$this->db->{$collection}->deleteIndex($keys, $options);
$this->_clear();
return ($this);
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.