MongoDB's projection feature being overlooked

I am completely new to Mongo, so I'm sure there's something obvious that I'm missing. Despite searching online, I can't figure out why my MongoDB query in a JavaScript file isn't working.

Strangely enough, the projection part of the query is being ignored by Mongo, everything else seems to be functioning correctly.


criteria = ' { "powersave_enabled" : false, "tx_rate" : { $lt : 26000 }, "rx_rate" : { $lt : 26000 }, "btyes-r" : { $ne: 0 } } ';

projection = ' {"_id":0, "hostname" : 1, "rssi" : 1, "mac" : 1, "ap_mac" : 1, "noise" : 1} ';

command = criteria + ', ' + projection;

accessPoints = db.cache_sta.find(command);

while (accessPoints.hasNext()){
  printjson( accessPoints.next() );
}

After printing and testing the command in mongo myself, it appears to be correct. There must be an issue with the JS code.

Any help would be greatly appreciated!

Answer №1

It is recommended to pass the criteria and the projection as objects instead of concatenating them, like shown below:

criteria = { "powersave_enabled" : false, "tx_rate" : { $lt : 26000 }, "rx_rate" : { $lt : 26000 }, "btyes-r" : { $ne: 0 } };

projection = {"_id":0, "hostname" : 1, "rssi" : 1, "mac" : 1, "ap_mac" : 1, "noise" : 1};

accessPoints = db.cache_sta.find(criteria, projection);

while (accessPoints.hasNext()){
     printjson( accessPoints.next() );
}

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

Is there a way to retrieve a file received in a response to a POST request using nodejs?

I'm encountering an issue with sending a POST command to download a file. I am attempting to send a POST request with a specific URL from the client side, along with a parameter that indicates the file to be downloaded. var req = $.ajax({ type: ...

How to use JavaScript to Hide the Chrome Print Preview Dialogue Box

Is there a way to close the print preview dialogue box opened by the window.print() method using JavaScript? I'm thinking about using Custom Events to dispatch the "esc key", but I'm not sure where to dispatch it on the element. https://i.sstat ...

What is the best way to ensure that an input tag within a fieldset tag is required

I am currently working on creating a sign-up page using HTML and JavaScript. However, I am facing an issue with making the input tag required in the HTML code. The following code snippet does not seem to be working as expected: <input type="email" requ ...

Container for grid template columns and responsive window in a single row

Imagine having around 250 divs with the class slider-item styled in a certain way. You have a responsive grid in CSS called A which arranges these divs as columns/items when the window resizes, with a minimum item width of 240px. Check out how it looks bel ...

How can I establish a relationship between two collections in Laravel using MongoDB?

Utilizing the laravel-mongodb package from https://github.com/jenssegers/laravel-mongodb for data manipulation in MongoDB. In my MongoDB database, I have two collections: books and categories. My ultimate goal is to import this data into MySQL. To achiev ...

Update the display using a button without the need to refresh the entire webpage

I am currently working on a website project that requires randomized output. I have successfully implemented a solution using Javascript, but the output only changes when the page is reloaded. Is there a way to update the output without refreshing the en ...

I'm having trouble using Discord.js to set up a custom role with specialized permissions for muting users

module.exports = { name: "mute", description: "This command is used to mute members in a server", execute: async function (msg, arg) { const muteRole = await msg.guild.roles.cache.find((r) => r.name == "Mute ...

When Vue 3 is paired with Vite, it may result in a blank page being rendered if the

Issue with Rendering Counter in Vite Project After setting up a new project using Vite on an Arch-based operating system, I encountered a problem when attempting to create the simple counter from the Vue documentation. The element does not render as expec ...

Issue with Vue JS: e.preventDefault not functioning correctly when using Axios

I am facing an issue in my Laravel project where I have implemented a method in the Vue instance to validate resource availability upon form submission. The validation is done through an AJAX call using axios, and if any resources are unavailable, I receiv ...

Exploring Angular 2's nested formGroups, formArrays, and template binding benefits

Here's the issue at hand: I have a complex form with nested formgroups, visualized in a "simplified" structure like this: -> MyForm (formGroup) -> Whatever01 (formControl - input) -> Whatever02 (formControl - input) -> Whate ...

Providing both app and io as parameters to a module

Extracted from my server.js: const app = require('express')(); const server = require('http').createServer(app); const io = require("socket.io").listen(server); server.listen(port, function(){ console.log('Server listening at ...

Retrieving outcomes from a sequence of callback functions in Node.Js

I've been struggling to get my exports function in Node.Js / Express app to return the desired value after going through a series of callback functions. I've spent hours trying to fix it with no success. Can someone provide some guidance? Here is ...

Incomplete Json information

As I embark on my journey to learn Javascript and work on building an application simultaneously, I can't help but feel optimistic about the learning process. To guide me through this venture, I've turned to the teachings of Alex MacCaw in his bo ...

What is the process for creating unique identifiers for tables in MongoDB?

When it comes to managing models with sails-mongo, I have encountered a problem with the unique: true attribute not functioning as expected. After searching through the bug tracker, it was mentioned that I need to manually add the unique option myself an ...

Guide to adding information to a file in Nodejs depending on a condition

Looking for assistance on how to append an annotation (@Circuit(name = backendB)) to a file if the "createEvent" name exists and the annotation is not already present. I am unsure of the process, so any help on checking and appending using streams would ...

What does "any[]" signify when used in a JavaScript/TypeScript array?

I am currently studying Angular2. During my lessons, I came across a requirement to save data into an "any" array. The example provided looked like this: import { Component } from '@angular/core'; import { GithubService } from '../../servi ...

Angular 4 scripts fail to execute after switching views

I am facing an issue with my Angular 4 app. All scripts work fine when the page is loaded for the first time. However, if I navigate to a different component within the app and then return to the main page, the scripts stop working. The console log shows a ...

Bring in dynamically

I am interested in dynamically importing a module only when it is needed. To achieve this, I have created a small mixin: import {extend} from "vee-validate"; export const rules = { methods: { addRule (name) { let requi ...

What is the reason behind every single request being treated as an ajax request?

Recently, I embarked on a new application development journey using rails 4.0. However, I've encountered an unexpected situation where every request is being processed as an ajax request. For instance, consider this link: =link_to "View detail", pr ...

Display an alert when no matches are found in autocomplete suggestions

I am implementing the code below to populate a textbox with data. If I input a, all records starting with a are displayed in the dropdown from the database. However, if I input a value that does not exist in the database, there is no message like "No Recor ...