Is there a way to locate a specific property within a mongoose model using a different property?

In my mongoose schema, I have defined the following properties:

     var personnelSchema = new Schema({

        fullName: String,
        dob: String,
        cNumber: String,
        address: String,
        wCard: String,
        dLic: Number,
        hrate: Number,

});

Is there a way to retrieve the value of the "hrate" property using only the "fullName" property? I do not have access to the ID, which would make it easier to retrieve the value.

Answer №1

If you want to learn about the mongoDB documentation regarding the find function, you can check it out here: https://docs.mongodb.com/manual/reference/method/db.collection.find/

For information on using mongoose for finding documents, visit this link: http://mongoosejs.com/docs/2.7.x/docs/finding-documents.html/

However, the common way to find documents by field is through the following code snippet:

Model.findOne({ fullName: 'someName'}, function (err, doc){
  // doc holds the Document object
});

Answer №2

To retrieve the hrate from the fullname property, you can utilize the projection method.

Use find to fetch all records and findOne to find the first matching record.

var projection = 'hrate' 

UserModel.findOne({
    fullName: "Some String"
  }, projection, function (err, data) {
    if (!data) {
      callback('No data found', null)
    } else {
      callback(err, data)
    }
})

// Query example for MongoDB shell:

> db.getCollection('test').findOne({fullName : "Hardik Shah"}, {'hrate' : 1})

If you require the full document along with the hrate, simply do not include the projection.

UserModel.findOne({
    fullName: "Some String"
  }, function (err, data) {
    if (!data) {
      callback('No data found', null)
    } else {
      callback(err, data)
    }
})

// Query example for MongoDB shell:

> db.getCollection('test').findOne({fullName : "Hardik Shah"})

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

Two draggable elements and two droppable containers all conveniently placed on a single webpage

My current project involves two sets of draggable elements and two sets of droppable elements. I'm trying to achieve a specific functionality where the first set of draggable elements can only be dropped inside the first set of droppables, while the ...

What is the correct way to apply a concatenated element id when using the .click() function in

Having an issue with assigning buttons to input boxes in a loop. When using concatenated element IDs with the .click() method, the buttons won't click for some reason. The following code works: document.getElementById("streamInput1") .addEventLi ...

Tips for iterating through an array of object literals and combining values with matching IDs

Looking for a more efficient way to iterate through an array of object literals and combine values in objects with duplicate IDs. Is there a smarter approach than utilizing multiple nested for loops? Here is the sample data provided: { "theList": [ ...

Outlining in Three.js

Can I achieve a black outline effect on my 3D models using three.js? I'm looking to create graphics similar to Borderlands 2, with toon shading and distinct black outlines. ...

Adding a CSS class to an HTML element using JQuery

I have a collection of tabs, and I want to dynamically add an <hr> element with the class "break-sec" when a certain class is active. This element should be placed within a <span> element with the class "vc_tta-title-text" after the text. Here ...

I seem to be struggling with hiding/showing a div element. Can you figure

I am in the process of creating a gallery that will display a div with images when a button is clicked. The issue I am facing is that when I have two buttons, only the last images are clickable. It's a bit difficult to explain, but you can see it in ...

What is the process for exporting/importing a variable in Node.js?

What is the correct way to export/import a variable in Node.js? I attempted to use export and import methods, but I received an error message stating that it should be a module. After changing the type to module in the JSON file, it then told me that requ ...

Unable to insert array into MongoDB

I am encountering an issue while trying to send an array to mongodb. The res.json(user) function is returning an empty biddingGroup:[], and the field biddingGroup never appears in the mongodb document. I have searched through stack posts and tried various ...

Displaying a component following data retrieval in Vue.js

I am currently working on consuming an API using axios. Here is the code I have so far: <select> <option v-for="value in values"> value.name </option> </select> // js data(){ values: [], }, create ...

Creating an infinite scroll with a gradient background: a step-by-step guide

I am currently working on a project to develop an infinite scrolling webpage with a dynamic gradient background that changes based on the user's scroll position. While researching, I came across some code for infinite scrolling using time and date. I ...

What is the method for adding line breaks to a JSON file?

I've been developing a Discord bot and I'm currently storing currency values in a json file. The functionality is working smoothly, but the issue I'm facing is that it's adding them to the json file in a single line which makes it diffi ...

Using JQuery to extract the unique identifiers of nodes within a tree view format for the purpose of storing data according to these identifiers

Using Angular to display data in the view, I have a sample code here. When a specific age group category is clicked, I want to populate a form with data and save the entire dataset, including the parent node IDs. I am facing an issue with fetching the pa ...

Is NextJS rendering solely on the server, or is it capable of rendering on both

Within my users.js JSX file, I have an exported component that looks like this: ... return <MainContainer keywords="users"> export default Users During the process of SSR/SSG, the browser displays the users HTML (comprising of <li> t ...

Is it possible to utilize fetch from the local drive in conjunction with App Router in the latest version of Next JS?

I encountered a 404 error and realized that the expected response from an on message listener had gone out of scope. Initially, I was working on creating an API for user authentication and was advised to convert it to route.js. So, I made the necessary ch ...

Tips for formatting dates in Angular 6

I am currently working on a function that displays real-time dates based on user input. Currently, when the user enters the input, it is displayed in the front end as follows: 28.10.2018 10:09 However, I would like the date to change dynamically based on ...

I have been unable to find a solution for the non-functioning jQuery (3.4.1 / 3.3.1) load() issue

I have been working with jQuery's .load() function Take a look at my code snippet: <html> <head> <meta charset="utf-8"> <title>load demo</title> <script src="https://code.jquery.com/jquery-3.4.1.js"> ...

Guide to utilizing JavaScript for a basic gaming experience

Looking to incorporate multiple divs that will vanish upon being clicked, while also increasing the score by 1 through javascript. Any suggestions on how to accomplish this? ...

JavaScript ES6 array method for generating an object from an array

I wish to transform the following array: [ { event: "LIB", block_calendar: "YES", obs: "Lorem Ipsum", status: "Block", }, { event: "LIB" block_calendar: "YES" o ...

Multi-line input in ExtJs

Is there a way to create a multiline input with vertical scrollbars in EXTJS? I tried the following code: noteField = new Ext.form.TextField({ emptyText: 'note...', multiline: true, applyTo: &apos ...

Can you provide the proper syntax for the Jquery done() function?

I'm currently dealing with the code below: However, I encountered an error on line 6: Uncaught SyntaxError: Unexpected token { Even after reviewing the documentation for the done() function, I'm unable to identify my mistake here. ...