Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6601173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:43:08+00:00 2026-05-25T18:43:08+00:00

I have a problem that I need some help on but I feel I’m

  • 0

I have a problem that I need some help on but I feel I’m close. It involves Lithium and MongoDB Code looks like this:
http://pastium.org/view/0403d3e4f560e3f790b32053c71d0f2b

$db = PopularTags::connection();

        $map = new \MongoCode("function() {
            if (!this.saved_terms) {
                return;
            }

            for (index in this.saved_terms) {
                emit(this.saved_terms[index], 1);
            }
        }");

        $reduce = new \MongoCode("function(previous, current) {
            var count = 0;
            for (index in current) {
                count += current[index];
            }
            return count;
            }");

        $metrics = $db->connection->command(array(
           'mapreduce' => 'users',
           'map' => $map,
           'reduce' => $reduce,
           'out' => 'terms'
       ));

        $cursor = $db->connection->selectCollection($metrics['result'])->find()->limit(1);
        print_r($cursor);
/**
User Data In Mongo

{
 "_id" : ObjectId("4e789f954c734cc95b000012"), 
"email" : "example@bob.com", 
 "saved_terms" : [
    null,

    [
        "technology",
        " apple",
        " iphone"
    ],
    [
        "apple",
        " water",
        " beryy"
    ]
] }


**/

I am having a user savings terms they search on and then I am try to get the most populars terms
but I keep getting errors like :Uncaught exception ‘Exception’ with message ‘MongoDB::__construct( invalid name ‘. does anyone have any idea how to do this or some direction?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T18:43:08+00:00Added an answer on May 25, 2026 at 6:43 pm

    First off I would not store this in the user object. MongoDb objects have an upper limit of 4/16MB (depending on version). Now this limit is normally not a problem, but when logging inline in one object you might be able to reach it. However a more real problem is that every time you need to act on these objects you need to load them into RAM and it becomes consuming. I dont think you want that on your user objects.

    Secondly arrays in objects are not sortable and have other limitations that might come back to bite you later.

    But, if you want to have it like this (low volume of searches should not be a problem really) you can solve this most easy by using a group query.
    A group query is pretty much like a group query in sql, so its a slight trick as you need to group on something most objects share. (An active field on users maybe).

    So, heres a working group example that will sum words used based on your structure.
    Just put this method in your model and do MyModel::searchTermUsage() to get a Document object back.

    public static function searchTermUsage() {
        $reduce = 'function(obj, prev) {
            obj.terms.forEach(function(terms) {
                terms.forEach(function(term) {
                    if (!(term in prev)) prev[term] = 0;
                    prev[term]++;
                });
            });
        }';
        return static::all(array(
            'initial' => new \stdclass,
            'reduce' => $reduce,
            'group' => 'common-value-key' // Change this
        ));
    }
    

    There is no protection against non-array types in the terms field (you had a null value in your example). I removed it for simplicity, its better to probably strip this before it ends up in the database.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need some help! I have no doubt this is a dumb problem that
I have an interesting SQL problem that I need help with. Here is the
I need to use something like get_or_create() but the problem is that I have
I have a problem and need some help. My application uses outlook to send
I have a piece of code here that i really could use some help
I'm writing a code formatter and I need some help. I have to find
The problem is this: I have multiple competing threads (100+) that need to access
I have a problem with reflection. I need to find the type that instantiates
i have very simple problem. I need to create model, that represent element of
I have a problem that I feel is best implimented in a stand alone

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.