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

  • SEARCH
  • Home
  • 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 1958354
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T08:18:21+00:00 2026-05-17T08:18:21+00:00

We use MongoDB to collect logs on pageviews. $collection_name = logs..date(‘Y’)…date(‘m’)…date(‘d’); $collection = $this->Mongo->$collection_name;

  • 0

We use MongoDB to collect logs on pageviews.

$collection_name = "logs.".date('Y').".".date('m').".".date('d');
$collection = $this->Mongo->$collection_name;
$collection->insert($pageview);

The code above creates a new collection for every day.

I would like to be able to create an index on the above collection when it is created. Is there anyway to do this?

  1. In a traditional RDBMS, this is accomplished through the schema. Is there anything similar in MongoDB? Is it possible to configure the database to create indexes on new collections?
  2. If not, what is the best way to accomplish this in PHP? I don’t want to call ensureIndex everytime I call insert
  • 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-17T08:18:21+00:00Added an answer on May 17, 2026 at 8:18 am

    You can pre-create them, since it’s not exactly a secret what collections you’ll need 🙂

    You could run something like:

    for ($month = 0; $month < 12; $month++) {
        for ($day = 0; $day < 31, $day++) {
           $c = $db->getCollection("logs.2010.$month.$day");
           $c->ensureIndex(array("foo" => 1));
        }
    }
    

    ensureIndex will create the collection if it doesn’t already exist.

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

Sidebar

Related Questions

Hello everyone I use MongoDB find() method like this: $cursor = $collection->find(); foreach($cursor as
i'm trying to use mongodb as a network wide configuration storage. This the same
I wrote a script to insert items into mongodb #!/usr/bin/perl use strict; use warnings;
I was trying to use the count() Mongodb feature (db.collection_name.count({data:value}) using the Ruby Driver.
I am developing an application that will use MongoDB running on Windows Azure. This
I have a good reason to use MongoDB for part of my app. But
Is it a good idea to use MongoDB in .NET desktop application?
I want to allow my shared hosting sites to use MongoDB, however as they
Are there any good applications that people use with the MongoDB database to make
use this website a lot but first time posting. My program creates a number

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.