What is the equivalent command in MongoDB?

Just starting out with MongoDB and I'm curious about the equivalent of this SQL statement:

SELECT * FROM users WHERE CONCAT(firstName, ' ', lastName) LIKE CONCAT('Walter Whi', '%')

Answer №1

SQL's CONCAT operation can be compared to MongoDB's aggregate method, while SQL tables correspond to MongoDB documents. The SQL SELECT statement is similar to MongoDB's Projection method.

For example, in MongoDB you can use the following code snippet to concatenate first and last names for users and then filter by a specific first name:

db.users.aggregate(    [
        { $project: { userDescription: { $concat: [ "$firstName", " - ", "$lastName" ] } } },
        { $match : { $firstName: "Walter Whi" } }    ] )

If you need more information, please visit the following documentation link: https://docs.mongodb.com/manual/reference/operator/aggregation/match/

Answer №2

If you want to see a similar example, check out the following:

db.users.insert({name: 'paulo'})
db.users.insert({name: 'patric'})
db.users.insert({name: 'pedro'})

db.users.find({name: /a/})  //similar to '%a%' output: paulo, patric
db.users.find({name: /^pa/}) //similar to 'pa%' output: paulo, patric
db.users.find({name: /ro$/}) //similar to '%ro'
db.students.aggregate(      
 { $project: {studentNo:"$studentNo" , name: { $concat: [ "$firstName", " ", "$lastName" ] },age:"$age"}},
  {$match:{ "name":/Suraj/}})

The 'project' command is used to display all columns along with concatenating two columns and then searching for new records.

In MongoDB, Aggregation functions similarly to the 'where' and 'having' clauses in SQL.

For a more comprehensive understanding, refer to the MongoDB Aggregation documentation

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

When transitioning to an object, Vue.js fails to refresh

My component contains an object called elements: elements: { id: { text: '', color: '', ... } To modify it, I use: <a-textarea autoFocus placeholder="text to include" :style="{ width: &ap ...

What is the process for incorporating Transformer instances into the buildVideoUrl() function using cloudinary-build-url?

This particular package is quite impressive, however, it seems to lack built-in support for looping gifs. Fortunately, the provided link demonstrates how custom URL sections like "e_loop" can be created. One challenge I'm facing is figuring out how t ...

Is there a way to display the JavaScript widget exclusively on the homepage or main page

How can I limit the display of a Twitter widget on my blog page to only show on the main page and hide it when visitors navigate to sub-pages or individual blog entries? ...

Deleting images based on their class through a loop

Currently, I am in the process of constructing a carousel using data retrieved from an endpoint. The challenge I face is determining the appropriate image size to request from the server. To address this, I perform some front-end processing to dynamically ...

Strategies for resolving the module not found error: Unable to locate '@mui/icons-material/Adb'?

I installed material-ui core using the command below: npm i @material-ui/core However, when running my reactjs code afterwards, I encountered this error message: Module not found: Can't resolve '@mui/icons-material/Adb' Can someone pleas ...

Can I run Angular, Flask, and MongoDB all with just the `npm start` command?

Our team is in the process of developing a web service that utilizes Angular for the front-end, Flask for the back-end server, and MongoDB as the database. When it comes time to launch our web service, we need to run three separate processes: npm start ( ...

Guide on transforming Div content to json format with the use of jquery

I am trying to figure out how to call the div id "con" when the export button is clicked in my HTML code. I want it to display JSON data in the console. If anyone has any suggestions or solutions, please help! <html> <div id ="con"> < ...

What are the differences between performing a search in MongoDB (database component) and Node.js (server-side)?

1) My input consists of a string with approximately 30 comma-separated elements, such as str1, str2, str3, etc. This is represented by "INPUT_STRING". 2) Within my MongoDB collection, I have a set of "allowed_strings" which includes entries like str1, str ...

Selecting elements with jQuery allows you to manipulate the

Encountering issues with jQuery selectors. This is how my HTML looks: <form method="" action=""> <p id="question_1"> <h1 id="question">1. A Question</h1> <div id="choices"> ...

Stop the form from submitting when the Enter key is pressed

I am experiencing an issue with my form that contains around 10 input text boxes. When I press the enter key from an input text box, it skips the text boxes in between and goes directly to the submit button. This problem occurs only when using the keyboard ...

The server has denied access to the resource due to a HTTP 403 Forbidden error in the AJAX request

I have exhausted all resources online in an attempt to resolve an error I am encountering with my AJAX request. Even after trying XMLHttpRequest() request, the problem remains unsolved. Below is the code for my Ajax Request: $.ajax({ type: "POST", url: " ...

My code seems to be malfunctioning

I'm attempting to conceal the chatname div. Once hidden, I want to position the chatid at the bottom, which is why the value 64px is utilized. However, I encountered an error stating The function toggle3(); has not been defined. <div id="chat ...

Synchronously updating multiple objects in Node with MongoDB

Using MongoDB, Node.js, Async.js, and Express.js I am currently working on updating multiple documents simultaneously in MongoDB using Nodejs. My goal is to wait for all documents to be updated before notifying the user that the process is complete. Howe ...

Meteor encountered an error while trying to insert the data: Access has been denied. There are no validation rules set on this specific collection to allow the method '

Hey there! I've recently started working on a Meteor-React project and I'm facing an issue while trying to add a new object to an existing collection. The error mentioned in the title keeps popping up. Below is the component responsible for inse ...

Issue arise when utilizing async/await in conjunction with .forEach method

My code is attempting to make a basic request to Redis in order to retrieve all the values (not keys) from my database. However, I am encountering an issue where the tab is being returned before the .forEach function even begins. This is evident because ...

Is it possible to execute a REST call in JavaScript without utilizing JSON?

(I must confess, this question may show my lack of knowledge) I have a basic webpage that consists of a button and a label. My goal is to trigger a REST call to a different domain when the button is clicked (cross-domain, I am aware) and then display the ...

My project is experiencing issues with the execution of JavaScript codes

Greetings, I'm having trouble with my JavaScript codes. The following code works perfectly: function Choice() { var box = document.getElementById('searchh'); if (!count) { var count = 0; } box.onclick = function () ...

Eliminate every instance using the global regular expression and the replace method from the String prototype

function filterWords(match, before, after) { return before && after ? ' ' : '' } var regex = /(^|\s)(?:y|x)(\s|$)/g var sentence1 = ('x 1 y 2 x 3 y').replace(regex, filterWords) console.log(sentence1) sentence2 ...

The html-duration-picker is not being displayed in the proper format

I've been working on integrating an external library that allows for inputting document length. Specifically, I'm using the html-duration-picker library, but it seems like the input functionality is not quite right for durations. Could it be th ...

The AngularJs Wizard Validation Inputfield is unable to locate the scope

Hello all, I am new to AngularJs and encountering an issue with my code. Currently, my HTML structure looks like this: <div class="wizardContainer"> <wizard on-finish="finishedWizard()"> <wz-step title="1"> <h1>Questio ...