mongodb-php

MongoDB - Aggregation - To get unique items in array

送分小仙女□ 提交于 2019-11-30 04:13:37
Here's my MongoDB collection: { "_id" : ObjectId("515d8f53175b8ecb053425c2"), "category" : "Batteries", "products" : [ { "brand" : "Duracell", "item" : [ "AA", "AAA" ] }, { "brand" : "Everyday", "item" : [ "9V", "AA", "12V" ] } ] } The output that I need is 1) Unique list of all items {["AA", "AAA", "9V", "12V"]} and 2. unique list of items per product { "category" : "Batteries", "item": ["AA", "AAA", "9V", "12V"] } I'm very new to MongoDB, and I tried different aggregations functions and nothing seems to work. Please help. Ananth After few more tries, I had solved this. Here's the commands:

Mongodb php get id of new document?

僤鯓⒐⒋嵵緔 提交于 2019-11-29 04:45:22
问题 Creating a document: $db->collection->insert($content); // $newDocID = ??? I'm trying to get the new document's id. How? Thanks. 回答1: According to the docs the array you pass to insert will be amended with an _id field: $db->collection->insert($content); $newDocID = $content['_id']; 回答2: You can also get _id before insert. Just add _id field to document with new MongoId ie. $content['_id'] = new MongoId(); $db->collection->insert($content); Also there are nice benefits of this: You don't need

Can't install mongo-php-driver on OS X 10.11

心已入冬 提交于 2019-11-29 03:49:48
问题 I've read through all the similar Stack Overflow questions - nothing addresses my specific issue. I'm running OS X 10.11 (El Capitan). I've cloned the mongo-php-drive repo and these commands succeed: phpize ./configure make But sudo make install fails: (master) ~/tmp/mongo-php-driver $ sudo make install Installing shared extensions: /usr/lib/php/extensions/no-debug-non-zts-20121212/ cp: /usr/lib/php/extensions/no-debug-non-zts-20121212/#INST@39898#: Operation not permitted make: *** [install

mongodb php - how to do “INNER JOIN”-like query

荒凉一梦 提交于 2019-11-28 08:43:09
I'm using the Mongo PHP extension. My data looks like: users { "_id": "4ca30369fd0e910ecc000006", "login": "user11", "pass": "example_pass", "date": "2010-09-29" }, { "_id": "4ca30373fd0e910ecc000007", "login": "user22", "pass": "example_pass", "date": "2010-09-29" } news { "_id": "4ca305c2fd0e910ecc000003", "name": "news 333", "content": "news content 3333", "user_id": "4ca30373fd0e910ecc000007", "date": "2010-09-29" }, { "_id": "4ca305c2fd0e910ecc00000b", "name": "news 222", "content": "news content 2222", "user_id": "4ca30373fd0e910ecc000007", "date": "2010-09-29" }, { "_id":

MongoDB, MapReduce and sorting

霸气de小男生 提交于 2019-11-27 19:54:20
I might be a bit in over my head on this as I'm still learning the ins and outs of MongoDB, but here goes. Right now I'm working on a tool to search/filter through a dataset, sort it by an arbitrary datapoint (eg. popularity) and then group it by an id. The only way I see I can do this is through Mongo's MapReduce functionality. I can't use .group() because I'm working with more than 10,000 keys and I also need to be able to sort the dataset. My MapReduce code is working just fine, except for one thing: sorting. Sorting just doesn't want to work at all. db.runCommand({ 'mapreduce': 'products',

MongoDB and CodeIgniter [closed]

前提是你 提交于 2019-11-27 16:50:26
Can anyone assist in pointing me to a tutorial, library, etc. that will allow me to work with MongoDB from CodeIgniter? I'm not sure if its the "CodeIgniter way" but I created a CodeIgniter library that extends the Mongo class with an extra property to store the current database connection. Here are the relevant code files from my project. config/mongo.php $config['mongo_server'] = null; $config['mongo_dbname'] = 'mydb'; libraries/Mongo.php class CI_Mongo extends Mongo { var $db; function CI_Mongo() { // Fetch CodeIgniter instance $ci = get_instance(); // Load Mongo configuration file $ci-

Numeric Collection Name Remove

六月ゝ 毕业季﹏ 提交于 2019-11-27 07:39:24
问题 How can you drop a numeric collection from MongoDB? PRIMARY> db.123456789011.remove({}); Tue Mar 20 08:42:51 SyntaxError: missing ; before statement (shell):1 PRIMARY> db.123456789011.drop({}); Tue Mar 20 08:43:13 SyntaxError: missing ; before statement (shell):1 Was created through a PHP script.. now I can't figure out how to remove this.. Thoughts? Thank you 回答1: This should work: db["123456789011"].drop() 回答2: I was having the same issue with a collection generated by a java class db["1234

Mongodb query specific month|year not date

江枫思渺然 提交于 2019-11-27 05:20:49
How can I query a specific month in mongodb, not date range, I need month to make a list of customer birthday for current month. In SQL will be something like that: SELECT * FROM customer WHERE MONTH(bday)='09' Now I need to translate that in mongodb. Note: My dates are already saved in MongoDate type, I used this thinking that will be easy to work before but now I can't find easily how to do this simple thing. JohnnyHK You can do that using aggregate with the $month projection operator: db.customer.aggregate([ {$project: {name: 1, month: {$month: '$bday'}}}, {$match: {month: 9}} ]); With

Calculate skip value for given record for sorted paging

一笑奈何 提交于 2019-11-26 23:02:36
I'm trying to calculate the skip value for a given record in a mongo db collection using the php driver. So taking a given record, find out the index of that record within the entire collection. Is this possible? Currently I'm selecting all records and manually doing an index of on the array of results. This is called "forward paging" which is a concept you can use to "efficiently page" through results in a "forward" direction when using "sorted" results. JavaScript logic included (because it works in the shell), but not hard to translate. The concept in general: { "_id": 1, "a": 3 }, { "_id":

MongoDB and CodeIgniter [closed]

旧街凉风 提交于 2019-11-26 18:46:10
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 5 years ago . Can anyone assist in pointing me to a tutorial, library, etc. that will allow me to work with MongoDB from CodeIgniter? 回答1: I'm not sure if its the "CodeIgniter way" but I created a CodeIgniter library that extends the Mongo class with an extra property to store the current database connection. Here are the