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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T19:12:13+00:00 2026-06-03T19:12:13+00:00

Due to a bug in my PHP script, I have created multiple erroneous entries

  • 0

Due to a bug in my PHP script, I have created multiple erroneous entries in my MongoDB. Specifically, I was using $addToSet and $each and under certain circumstances, the MongoDB object gets updated wrongly as below:

array (
  '_id' => new MongoId("4fa4f815a6a54cedde000000"),
  'poster' => 'alex@randommail.com',
  'image' => 
  array (
    '0' => 'image1.jpg',
    '1' => 'image2.jpg',
    '2' => 'image3.png',
    '3' => 
    array (
      '$each' => NULL,
    ),
  ),

You can see that “image.3” is different from the other array entries, and that was the incorrect entry. I have fixed the related in my PHP script, however I am having difficulties tracking down all the affected entries in MongoDB to remove such entries.

Is there any way in MongoDB to check if any of the image array entry contains another array instead of string? Since the index of containing the sub-array is a variable, it would not be possible to perform a $type check on image.3 for every entry.

Could use any suggestion. Thanks!

  • 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-03T19:12:14+00:00Added an answer on June 3, 2026 at 7:12 pm

    Since this was an error in your PHP, I suppose, now you want to upgrade all erroneous documents.

    If so, you could just find all documents where array item is an array itself.

    > db.arr.insert({values: [1, 2, 3, [4, 5]]})
    > db.arr.insert({values: [6, 7, 8]})
    > 
    > db.arr.find({values: {$type: 4}})
    { "_id" : ObjectId("4fae0750d59332f28c702618"), "values" : [ 1, 2, 3, [ 4, 5 ] ] }
    

    Now let’s fix this. To remove such entries without fetching them to PHP I offer this simple two-step operation.

    First, find all documents with arrays and unset those arrays. This will leave nulls in place of them. Note, it will only match and update first array in the document. If there are several, you want to repeat the operation

    > db.arr.update({values: {$type: 4}}, {$unset: {'values.$': 1}}, false, true);
    > db.arr.find()
    { "_id" : ObjectId("4fae0750d59332f28c702618"), "values" : [ 1, 2, 3, null ] }
    

    Remove nulls.

    > db.arr.update({values: null}, {$pull: {values: null}}, false, true);
    > db.arr.find()
    { "_id" : ObjectId("4fae0750d59332f28c702618"), "values" : [ 1, 2, 3 ] }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Due to a bug in my servers PDO version, I have switched to using
Due to a bug in Flash, I have to use the ASPXAuth cookie to
I have a C++ program that crashed due to a bug. It would not
All, Due to a bug in a library I'm using, I need to override
I have a long-running process where, due to a bug, a trivial/expendable thread is
I have a ComboBox which I cannot bind directly to (due to this bug
Due to a bug (perhaps in the numpy distribution I'm using), I can't use
I installed ImageMagick via Homebrew . However, due to a bug I have with
I recently had a bug in Firefox due to me choosing the wrong script
I have the follow code: window.onbeforeunload = function() { $.post( 'script/ratings.php', { data: dataToServer,

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.