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 8354271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:30:02+00:00 2026-06-09T09:30:02+00:00

When using mongo shell I can run something like this: db.sandbox.insert({line : db.eval(‘storedFunction()’) })

  • 0

When using mongo shell I can run something like this:

db.sandbox.insert({"line" : db.eval('storedFunction()') })
       or
db.sandbox.insert({"line" : function() {return 1337} })

I was unable to achieve the same effect using php.
What I wish to do is run something similar to

$fnc = "function() {return 123}";
$col = (new Mongo)->database->collection;
$col->insert(['col' => new MongoCode($fnc)]);

and find

{
   "_id" : ...., 
   "col" : 123
} 

in the collection. How can I achieve this? Is it even possible?


If you want even more details I’m trying to imitate autoincrement. This function that I’m writing should retrieve the last used value of incremental field, increment it and return the value thus assigning a unique value to the document that is being inserted.

  • 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-06-09T09:30:04+00:00Added an answer on June 9, 2026 at 9:30 am

    You can’t do what you want with MongoDB, exactly. When you run your example in the shell, it calls the db.eval... bit first, then passes the computed result as part of the document. The only thing Mongo will evaluate during a write operation are the special modifier fields as part of an update (or upsert, if you want). None of those will let you do a per-collection counter, though.

    For autoincrement, you’ll need to do two queries. You can use findAndModify to atomically increment and retrieve a number, then assign it to your lines value.

    Here’s how you’d do it from the shell:

    counter = db.counters.findAndModify({
      query: {_id: "line-counter"}, 
      update: {$inc: {value: 1}}, 
      new: true, 
      upsert: true}).value
    db.sandbox.insert({line: counter})
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can run the script in the specific database like mongo main myscript.js<br/> where
I am using PHP/Mongo to query a data set like the following: { description:
I am using the Mongo Ruby driver and have this block of Ruby code
Using Mongo and Rails, I would to build a friendship system like facebook: -
So, I'm sure I'm missing something simple here, but when I run mongo as
I'm using the Mongo shell to query my Mongo db. I want to use
I saw how this could be accomplished from the mongo shell here: MongoDB: Updating
I'm using the Mongo LINQ Driver for C#, works great. Sorting a lot of
I am trying to create a report using the mongo connector on iReport designer
I'm using perl and MongoDB::GridFS (along with other mongo modules) to store and retrieve

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.