The MongoDB bitwise operation does not return any results when checking for a

I am trying to perform a bitwise check on my data without returning any data. The default value for Rights parameter is 1. How can I resolve this issue?

db.getCollection('forms').find(
{ 
    "IsActive" : true, 
    "$or" : 
    [
    { "$where" :  "(this.Acls.Rights & 1) == 1" , "Acls.Actor._id" : ObjectId("5565f392a6df191eb4689bec") },
    { "$where" :  "(this.Acls.Rights & 1) == 1" , "Acls.Actor._id" : ObjectId("5565f392a6df191eb4689bed") }
    ] 
 }
)

Below is the document that should match:

{ 
    "_id" : ObjectId("55686c44a6df1a1008c0b148"), 
    "IsActive" : true,
    "Acls" : [ 
        { 
            "_id" : ObjectId("557820b1a6df1a032c2c643a"), 
            "IsActive" : true,
            "Actor" : { 
                "_id" : ObjectId("5565f392a6df191eb4689bec"), 
                "IsActive" : true,
                "Name" : "admin",
                "TypeId" : 2
            }, 
            "Department" : null, 
            "Rights" : NumberLong(1)
        }
    ], 
    "AclCount" : 1
}

Answer №1

Your approach has an issue due to the limitations of JavaScript evaluation when compared to MongoDB's native search capabilities, especially concerning "dot notation." You should use JavaScript methods to evaluate each array element's properties. The best method for this scenario is using the built-in JavaScript function ".some()" which returns true or false based on whether any array element meets the condition, stopping once a match is found without checking all elements. Unfortunately, comparing ObjectId in a matching pair as you intended won't work here unless you adjust your logic accordingly. Typically, you'd utilize "$elemMatch" to ensure both properties are met for a specific array element. However, since you can't nest a "$where" clause inside "$elemMatch," all logic must be handled in JavaScript: db.forms.find({ "IsActive": true, "$or": [ { "$where": "return this.Acls.some(function(el) { return ( el.Rights & 1 == 1 ) && el.Actor._id.equals(ObjectId("5565f392a6df191eb4689bec")) });"} { "$where": "return this.Acls.some(function(el) { return ( el.Rights & 1 == 1 ) && el.Actor._id.equals(ObjectId("5565f392a6df191eb4689bed")) });"} ] }) Due to performance issues with $where (as it cannot utilize indexes), I recommend exploring alternative methods for ACL matching instead of the bitwise approach you're currently using. While other options may incur higher storage costs, the advantage lies in quicker query results by directly matching elements with an $elemMatch clause.

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

"Implementing a feature in AngularJS to dynamically display markers on a Google Map based on latitude and

I am a newcomer to AngularJS and I am attempting to pass my JSON latitude and longitude based on ID into the Google API. Here is the structure of my JSON file: { "totalCount":206, "deals":[{ "id":"2", "Name":"samir", "locations":[{ ...

Implementing Github Oauth2 in a Rails server independent from a chrome extension

Looking to implement Github Oauth2 from my chrome extension, but rather than using chrome.identity.launchWebAuthFlow I want to handle it through my server. This way, I can avoid exposing my client ID and Client Secret in the javascript of the extension. My ...

Trail of crumbs leading to pages displayed in a div container

My website is designed with only one page where all other pages are loaded within a div. I am looking to implement breadcrumbs to keep track of the pages loaded inside the div. However, the solutions I have found online only work for pages loaded in the en ...

Adjust the values of a specific field in every document by referencing another field within the same document

Within my database, I store student records that each contain the fields amountOwed and tuition. The task at hand is to update the value of amountOwed to be the sum of its current value and tuition. I am considering utilizing a .find query to retrieve al ...

Loop through two sets of data and perform multiplication on specific attributes when they match

I have a unique item that looks like this Object {SD00002:1,SD00011:3,SD00002:6} The values of the properties are set automatically. Currently, I have an array of similar objects as shown in the image below: https://i.sstatic.net/pSkvf.jpg My goal is to ...

Incorporate animated SVG directly into Vue templates without using any additional enclosures

I need to incorporate dynamic SVG code into my <template> without using v-html or any wrapper around it. The desired end result should look like the following, but template does not support v-html to achieve this. If there is a way to accomplish thi ...

Unable to focus on the same DOM element multiple times - Vue.js

Apologies for any syntax errors, although the code is functioning perfectly. There may have been a mistake during the copying process. Situation: I am facing an issue with a component called 'dropdown', which is repeated three times using v-for= ...

Using the setTimeout function with asynchronous tasks

I have a situation where I need to introduce a 5000ms delay before firing an asynchronous function. To accomplish this, I attempted to utilize the setTimeout() method. This async function is called within a loop that runs multiple times, and each time it i ...

Can a rotation animation be incorporated into an image in next.js when it is clicked?

Looking to enhance a next.js image with an animation? How about making it rotate 360 degrees upon each click? Despite my attempts at toggling classes, I can't seem to achieve the desired outcome. Any guidance would be greatly appreciated. Thank you in ...

"Encountering an issue where the route function in Passport and ExpressJS is not being

When using passport for admin authentication, I am facing an issue where the redirect is not calling my function. Consequently, all that gets printed on login is [object Object] Here is my code: app.get('/admin', isLoggedIn, Routes.admin); ...

Tips for positioning a highcharts pie chart and legend in the middle of a page

I'm struggling to center my highchart data in a node/react single page application. Currently, it appears off-center like this: https://i.stack.imgur.com/ccR6N.png The chart is floating to the left and I would like to have everything centered within ...

Should the .js file type be omitted in the <script> tag for semantic accuracy?

Upon reviewing some inherited code, I noticed that JavaScript files are being linked in the following format: <script src="/js/scriptfile" type="text/javascript"></script> However, I was expecting to see: <script src="/js/scriptfile.js" ...

Creating a registration system using MongoDB and PHP

This code snippet only shows the title with a blank page. I have tested it without any data provided. Upon testing, the following errors are observed: Notice: Undefined variable: databse in C:\Winginx\home\localhost\public_html\re ...

Proper method for calling a function within a specific scope

I am utilizing user-contributed modules that I aim to avoid editing in order to make upgrades easier. My goal is to enable users to browse for a CSV file on the local filesystem, parse it, and display it in a dynamic table. For this task, I am using PapaP ...

Using Javascript to display or conceal a specific child element within a loop, based on which parent element has been clicked

I need assistance with creating a grid of projects where clicking on a specific 'project' in the loop will display the corresponding 'project_expanded' div, while hiding all other 'project_expanded' divs. My initial plan was ...

The issue persists with the $slice function in MongoDb

I am attempting to retrieve records using an aggregate function in MongoDB, but I keep encountering the error message stating that the operator $slice is invalid: db.getCollection('test').aggregate( [ { $match: { 'subjectId': &apos ...

Is it possible for the Mongodb find method to handle dynamic queries?

I am new to mongodb and would appreciate some advice on how to efficiently write the following query. In my collection, I have fields for location and date. There are 4 search conditions: Users can search without any parameters. Users can search with da ...

Encountered a connection error in the Spring Boot application: net::ERR_CONNECTION_REF

Currently working on a school project developing a Spring Boot application using Restful. The application runs smoothly locally, but when deployed to AWS, I am encountering an "net::ERR_CONNECTION_REFUSED" error for all my GET and POST requests sent to the ...

Preserve jQuery-enhanced webpage changes permanently

I am looking to permanently save modifications made on an HTML page using JQuery. I have come across suggestions about achieving this by sending an Ajax call and storing the data in a database table. However, I am unsure about what exactly needs to be save ...

What is the best way to obtain the accurate innerHeight of a div on the initial attempt

Within my webpage, there is a hidden sub navigation with its height initially set to 0. This sub navigation contains various sections of sub navs. When a section is clicked, I obtain the name of that section and then retrieve the innerHeight of the corres ...