An effective method for verifying information in MongoDB and generating new records if they are not already present

Is there an efficient way in MongoDB to search for the presence of new data, such as a phone number, and insert it into another collection if it does not already exist? For example, searching whether the phone numbers [123456789, 15, 20] associated with a user already exist. If numbers 15 and 20 do not exist in the database collection, I need to be able to identify this so I can insert them into another collection. I am looking for a solution that minimizes queries to the database for efficiency.

User Collection

{
    "_id" : "mJPqhyyGoeyfa3p2w",
    "createdAt" : ISODate("2015-11-30T22:33:27.649Z"),
    "phone" : {
        "number" : "123456789",
        "verified" : true
    },
    "profile" : {
        "picture" : "xx",
            "name" : "User IOS Mobile"
    }
}

Answer №1

To transfer all elements that are not present in the 'lineNo' array to a different collection, you can utilize the $project operator along with the $setDifference operator. The .aggregate() method allows access to the aggregation pipeline for this purpose.

For the insertion process, you will need to employ "bulk" operations.

var bulk = db.LineNoNotFound.initializeUnorderedBulkOp();
var count = 0;

db.inventory.aggregate([
    { '$group': {
        '_id': null,
        'phones': {'$push': '$phone.number'}
    }},  
    { '$project': {
          'notFound': {
             '$setDifference': [ [ 5, 15, 20, 25 ], '$phones' ]
         }
    }},
    { '$match': { 'notFound.0': { '$exists': true } } }
]).forEach(function(doc) {
    doc.notFound.forEach(function(num) {
        bulk.insert( { 'lineNo': num } );
        count++;
    });
    if (count % 100 === 0) {
         bulk.execute();
         bulk = db.LineNoNotFound.initializeUnorderedBulkOp();
    }
});

if (count > 0)  bulk.execute();

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can I fix the issue of the onClick function in my React list not activating the toggle?

How to Activate Toggle Functionality Currently, I am working on a project using React where I developed a list with active states. I have implemented the onClick functions, but unfortunately, they are not working as intended. When I click on the list item ...

Testing an ajax-driven website: what's the best approach?

Seeking to develop automated tests for my completely ajax-based website developed using Spring MVC/Java. I possess a basic understanding of Selenium and have managed to create some tests using this tool. The main issue lies in the fact that testing an aja ...

Why is the console log not working on a library that has been imported into a different React component?

Within my 'some-library' project, I added a console.log("message from some library") statement in the 'some-component.js' file. However, when I import 'some-component' from 'some-library' after running uglifyjs with ...

Is there a way to verify that a form field has been completed?

Currently, I am grappling with a method to clear a field if a specific field is filled in and vice versa. This form identifies urgent care locations based on the information provided by users. The required entries include the name of the urgent care facil ...

What is the best way to store jQuery selections in a cache?

I have a need to cache approximately 100 different selections for animating. Provided below is sample code. Is there an issue with the syntax in the second example? If this isn't the correct way to cache selections, it's definitely the most popul ...

Strategies for refining document types in mongoDB

Attempting to filter by type in mongoDB for "vod", "playlist", "series" This is how I'm trying: let filter = { type: "vod" && "playlist" && "series" } let video = await Video.find(filter); Unfortunately, it's not functioning as expected. ...

Troubles arise with loading Ajax due to spaces within the URL

I currently have two python files named gui.py and index.py. The file python.py contains a table with records from a MYSQL database. In gui.py, there is a reference to python.py along with a textfield and button for sending messages. Additionally, the g ...

Activate the Bootstrap Jquery/Ajax inline editing feature by simply clicking on the Edit button

Seeking recommendations for a way to implement inline editing. When the edit button is clicked, I want the label's content to be replaced with an input text field that can be updated in my MySQL database. This is what my code looks like: <label s ...

Obtain every possible sequence of US phone number segments for a provided number

The format of a US phone number is as follows: (XXX) XXX-XXXX Given a string consisting only of digits with a length between 0 and 10, I want to generate an array of all possible chunks that match the US phone number format. For example: Input: "54" Out ...

Using External APIs in React: A Guide

Recently, I created a React app using the npm module 'create-react-app' I ran into an issue where I needed to call an external API from api.example.com, but found that Axios was making requests to localhost instead of the external API. Here is ...

It seems that sessionStorage and localStorage reset after refreshing the page

I have a local document for a project that doesn't run on a web server. It contains multiple pages, each with a sidebar that can be toggled open or closed. I have implemented sessionStorage to save the current state of the sidebar (open or closed), bu ...

The error message popping up reads as follows: "TypeError: _this2.setState cannot

I am encountering an error that I don't quite understand. How can I resolve this issue and why is it occurring in the first place? Although I am receiving the correct response from the API, I am also getting an error immediately after making a call. ...

In Typescript, it is not possible to use generics as a function parameter in a

Looking for a solution regarding passing the union of two specific samples of generics to a function. The function in question is as follows: function myCommonFunc<T>({ data, render, }: { data: T; render: (data: T) => number; }) { return ...

Exporting JavaScript formatting in Netbeans is a breeze

Does anyone know how to preserve the formatting for JavaScript in Netbeans 8.1 when exporting? After clicking on the export button and expanding Formatting, I couldn't find any option specifically for JavaScript. I've thought about locating the ...

Saving a jimp resized image in a mongoDb database: A step-by-step guide

Does anyone have advice on saving a resized image using Jimp directly to MongoDB database in a Node.js project with Express, Multer, and GridFS? The write method I am currently using only writes to a folder. ...

Reversing the Jquery checkbox functionality

Seeking some quick assistance here. I'm puzzled as to why this function seems to be working in reverse. Whenever I try to check the checkboxes, they all get unchecked and vice versa. Everything was functioning smoothly until I introduced the .click() ...

Setting up Webpack to compile without reliance on external modules: A step-by-step guide

I am facing an issue with a third-party library that needs to be included in my TypeScript project. The library is added to the application through a CDN path in the HTML file, and it exports a window variable that is used in the code. Unfortunately, this ...

What could be causing my jQuery to not retrieve the value of the <select> element?

Here's a snippet from my index.html file: <body> <select id="car"> <option value="TOYOTA">TOYOTA</option> <option value="BMW">BMW</option> </select> <input type=button id="get_btn" valu ...

Controller in EmberJS is unable to access model data passed from route

Could someone lend me a hand with this concern? I currently have EmberJS 3.4 version and in my route, the code looks like: export default Route.extend({ model(){ const items = [{price: 10}, {price: 15}] return items }, }); Howeve ...

implement a dropdown feature for a vertical menu with the help of Jquery

Struggling to implement a dropdown feature on a vertical navigation using Jquery. The HTML includes a nav section with 1st level list items and nested li's for second level menu items. On clicking the 1st level li, the intention is to toggle SHOW/HID ...