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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T23:53:14+00:00 2026-06-07T23:53:14+00:00

There are 2 array. $director = array(name => James Cameron); $cursor = collection(persones)->find( array(name

  • 0

There are 2 array.

$director = array("name" => "James Cameron");
$cursor = collection("persones")->find( array("name" => "James Cameron") )->limit(1);
if ($cursor->count(true) == 0) {collection("persones")->insert($director);}

$actor = array("name" => "James Cameron", "name" => "Arnold Schwarzenegge");
$cursor = collection("persones")->find( array("name" => "James Cameron") )->limit(1);
if ($cursor->count(true) == 0) {collection("persones")->insert($actor);}

Adding all the directors.
Do check if there are people in the database, if not, then add.
Since the addition of an asynchronous, then the director has not yet been added, and the addition of an actor, added a 2nd time.
James Cameron is added to the base of the 2nd time.

  • 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-07T23:53:16+00:00Added an answer on June 7, 2026 at 11:53 pm

    It looks like what you want to do is insert a number of documents, making sure that there is only one entry for each distinct value of the ‘name’ field.

    There are two common ways to do this in MongoDB.

    The first is to create a unique index on the ‘name’ key and to do your inserts using ‘safe’ mode. If you do the insert that way, PHP will throw an exception when you try to insert a duplicate value.

    // In the mongo shell:
    db.test.ensureIndex( {name: 1}, {unique:true} );
    
    // In PHP:
    $data = array('name' => 'Art Pip');
    $options = array('safe' => true );
    
    try {
        $collection->insert($data, $options);
    } catch (Exception $e) {
        echo "Insert failed\n";
        echo "Error message: ".$e->getMessage()."\n";
        echo "Error code: ".$e->getCode()."\n";
    }
    

    Reference: http://www.php.net/manual/en/mongocollection.insert.php

    Note that if you’re querying on ‘name’, you’ll want to have an index on it anyway for performance reasons. Making it a unique index adds essentially no overhead to this process.

    The second way is to do an “upsert”. This is an update command which says “update this document if it exists, and create it if it does not exist yet”. If you do the insert this way, you also won’t get any duplicates.

    // In PHP
    $data = array('name' => 'James Cameron');
    $criteria = array('name' => 'James Cameron');
    $options = array('upsert' => true, 'safe' => true );
    
    try {
        $collection->update($criteria, $data, $options);
    } catch (Exception $e) {
        echo "Insert failed\n";
        echo "Error message: ".$e->getMessage()."\n";
        echo "Error code: ".$e->getCode()."\n";
    }
    

    Reference: http://www.php.net/manual/en/mongocollection.update.php

    By the way — there’s nothing that prevents you from using both techniques. You can use the ‘upsert’ for speed, and put in the unique index as an additional check.

    As a final note, the code you’re using doesn’t do what I think you think it does.

    If you run the following code:

    $actor = array("name" => "James Cameron", "name" => "Arnold Schwarzenegger");
    print_r($actor);
    

    You’ll see the following output:

    Array
    (
        [name] => Arnold Schwarzenegger
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There is an array of user states stored in the session. This works: <?php
Scenario: If there is an array of integers and I want to get array
I am making a page in which there will be array of images. I
What is the appropriate function that shows how many are there in an array?
I have a really tough situation in here. There is an array of these
I'm trying to write a snake game. It's pretty simple, there's an array of
There is an associative array with only one pair key=>value . I don't know
There is a string array myDownloadList containing 100 string URIs. I want to start
So there is this function Array.prototype.containsCaseInsensitive = function(obj) { var i = this.length; while
Hi there I have written a code that ietrates through whole array and copies

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.