Retrieve all key:value pairs from Mongo except for the specific pair you are excluding

Seeking advice on how to retrieve all items in a collection while excluding one specific key:value pair?

The structure of my collection is as follows:

{ "_id" : "HsM4HpwrYAXh2PJeN",
  "contact" : [ {
    "emailAddress" : "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="365e5744444f764659424253441855595b">[email protected]</a>",
    "someContact" : "No",
    "creationDate" : "N/A",
    "hardBounceBack" : "N/A",
    "unsubscribed" : "No"
  } ]
}

I have a total of 500 contacts in my “contacts” collection. My goal is to retrieve all contacts except for those with the value "unsubscribed":"No". I've experimented with $nin, $ne, and $where but haven't been able to find the correct approach to return everything excluding objects where "unsubscribed":"No".

Answer №1

To filter through a nested array of objects, it's important to utilize $elemMatch: along with $ne:

collection.find({ contact: { $elemMatch: { unsubscribed: { $ne: "No" }}}});

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

TS2688 Error: Type definition file for 'tooltip.js' not found

Why am I getting an 'undefined' error when trying to import the Tooltip class from the npm tooltip.js package in my TypeScript file? ...

Supervising the clash of schedules between parents and their children's commitments within

Here is a snippet of code for you to examine: <div onmousedown="alert('onmousedown');"> <div onclick="alert('onclick');"></div> </div> I am curious, is it possible to disable the onmousedown event while the ...

Trouble accessing HTML file in web browser

I just finished creating a basic webpage called HelloWorld.html using HTML, JavaScript, and CSS. It runs smoothly when I open it from Eclipse IDE and view it through a browser. The functionality of the page is that it contains 5 buttons, each revealing th ...

jQuery Mobile dual range slider functioning but experiencing some glitches

I successfully created a dual range slider by stacking two sliders on top of each other using the jQuery Mobile framework. In addition, I have implemented JavaScript to ensure that the left thumb does not exceed the right one. However, I have encountered ...

The reliability of innerHTML cannot be guaranteed as it does not consistently run synchronously

For a live demonstration of the issue, visit this jsbin. When the button is clicked, it triggers the buttonHandler() function, which is outlined below: function buttonHandler() { var elm = document.getElementById("progress"); elm.innerHTML = "thinking ...

Interacting with API through AngularJS $http.get

I am a beginner in AngularJS and I am trying to grasp its concepts by studying example codes. Currently, I have found an interesting code snippet that involves the $http.get function. You can find it here: I attempted to replace the URL with my own, but ...

Utilize JavaScript to assign a unique color to each category

I'm currently working on a JavaScript task My goal is to assign specific colors to different categories For example: if category name = x then color = blue if category name = y then color = red ... I attempted the following code, but it seems like ...

What is the process for using populate with a custom ObjectId that is of type String?

My current setup involves using a firebase project for authentication and saving additional user information in MongoDB. The challenge comes in when assigning the UID of the firebase user to the _id field of the user model in MongoDB. To make this possible ...

Excluding certain objects from JSON data in d3js

In my JSON file, I have the following data structure: { "children": [ { "name": "المصاريف", "children": [ {"name": "بنزين","size": 14230,"colour": "rgb(220,230,180)"}, {"name": "تاكسي","size": 25220,"colour": "rgb(22 ...

Using setTimeout() within a loop

set = [500,1000,1500,2000]; for (var j = 0; j < 4; j++) { setTimeout(console.log('Hi'), set[j]);} What could be causing this code not to display 'Hi' after the specified intervals in the list? ...

Using Mongoose's `findOne` along with `save` will generate a fresh object and activate

I am currently using MongoDB 4.4.10 (due to limitations on Synology). My stack includes Express, Mongoose, and UniqueValidator 3.0.0 which seems to be the main suspect in my issue. The problem arises when executing the following code: router.get('/te ...

Craft an individualized identifier using Mgo

Delving into the world of GoLang and MongoDB, I've embarked on creating a small web application - a blog to be precise. This marks my initial venture into new languages, and despite some initial hiccups with MGO, everything seems to be functioning smo ...

Utilizing JSON Parsing in JavaScript and jQuery

func({ "query": { "count": 1, "created": "2013-05-03T06:20:01Z", "lang": "en-US", "diagnostics": { "publiclyCallable": "true", "cache": { "execution-start-time": "32", "execution-stop-time": "32", "exe ...

Mapping data locations in MongoDB using RestKit

I'm encountering difficulty grasping the process of mapping location data stored in Mongo DB using RestKit. Here is the JSON data that I will need to map: { "name" : "TestPoint2", "media_resource" : "tester", "added" : ISODate("2012-10-10T23:00:00Z" ...

Ways to trigger a click event when selecting an item from a dropdown menu

Whenever a selection is made in my dropdown list, I want it to perform a specific action. The issue is that the SelectedIndexChanged event is triggered every time the page reloads, even if the selection hasn't changed. To ensure the action only occurs ...

Adjust the dimensions of the viewport in WebdriverJS

Is there a way to launch the Chrome browser with a specific viewport size, like 800x600? I have tried the following code: var width = 800; var height = 600; driver.manage().window().setSize(width, height); However, when running window.screen.width in the ...

Discover the method of using jQuery to identify a modification on a select element with an ID that is generated dynamically, and modify the choices available for selection

Our team has successfully implemented a user "profile" page with a two-column HTML table using a web framework (Rails 4.2.x). Users can dynamically add rows to this table by clicking an "add row" button, and everything is functioning as expected. The two c ...

Finding documents within an array in Mongoose: A comprehensive guide

Can you show me how to correctly use the mongoose.find() method to find this specific code? Paid.find({info:{data:"status"}}) I've tried the above code but it's just returning an empty array. info{ event: 'charge.success', ...

Attempting to incorporate a factory within a restricted function

Just starting out with AngularJS and I have a question. Can I use a factory code (logger) inside a private function like the example below? I'm still working on understanding angular concepts. Thanks for your help: (function () { 'use strict ...

JavaScript Regex: Removing substrings with spaces from a given string

I have a string that reads, "Hello @William Turner. how are you?" and I want to replace the term "@William Turner." with "@William Turner." After the replacement, the updated string should be "Hello @William Turner. how are you?" I tried achieving this u ...