How can I transform a string into an array in MongoDB?

I need to extract specific values from an array of objects within a string value. For example, I want to filter based on conditions like stage===1 && state==="not-started". The structure of the string Array is shown below.

"sts" : "[{\"percentage\":0,\"stage\":\"Stage1\",\"timestamp\":\"2020-02-11T09:52:53.018Z\",\"state\":\"not-started\",\"information\":\"Stage1 not started yet\",\"order_no\":0,\"display-name\":\"UNKNOWN ALL\"},{\"percentage\":0,\"stage\":\"Stage2\",\"timestamp\":\"2020-02-11T09:52:53.018Z\",\"state\":\"not-started\",\"information\":\"Stage2 is not started yet\",\"order_no\":1,\"display-name\":\"Stage2\"},{\"percentage\":0,\"stage\":\"Stage3\",\"timestamp\":\"2020-02-11T09:52:53.018Z\",\"state\":\"not-started\",\"information\":\"Stage3 is not started yet\",\"order_no\":2,\"display-name\":\"Stage3\"},{\"percentage\":0,\"stage\":\"Stage4\",\"timestamp\":\"2020-02-11T09:52:53.018Z\",\"state\":\"not-started\",\"information\":\"Stage4 is not started yet\",\"order_no\":3,\"display-name\":\"Stage4\"}]"

Answer №1

To change a string into an array, you can follow these steps:

let newArray = JSON.parse(yourData.variable);

In this case, variable represents the key in the data object provided as an example.

After that, you can apply a filter like this:

let filteredData = newArray.filter(({category, status}) => category==="Category1" && status==="ongoing")

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

Seeking a light-weight, text-rich editor specifically designed for use on our enterprise website. This editor should be even lighter than TinyMCE

I am in search of a lightweight text editor for an enterprise website, lighter than tinymce with basic buttons for the comment form. It is imperative that the editor also functions properly in IE6. So far, I have tried cleditor 15KB, but it has an issue in ...

"Utilize d3.js to selectively filter through a nested array of objects based on specific object

I have collected a dataset of meteorite landings and organized the data into four keys based on type: var dataByType = d3.nest() .key(function(d) { return d.rectype; }) .entries(dataset); // original dataset You can see the result ...

How can I extract the text enclosed in <li> tags using JavaScript?

Can someone help me with retrieving the text content of li tags? I managed to get a list of lis, but I'm having trouble assigning the text content to my country object. Snippet var lis = document.getElementById("navbar").getElementsByTagName("l ...

Preventing a user from navigating away from a page without completing a specific action, such as clicking a submit button

I am in the process of developing an interactive quiz platform. The quiz includes a timer that begins counting down once the user initiates the quiz. Upon completing the quiz, the user is expected to submit their answers. If the user runs out of time, th ...

How to align scrolling images with their scroll origin - a step by step guide

Curious to know the name of the effect where images scroll in a different orientation than the page, creating a 2D appearance. Take a look at the Google Nexus website and scroll down - do you see the effect? What is this effect called? Is it created usin ...

The parameter passed to the parser as an argument of [object Object] was found to be an invalid GraphQL DocumentNode. It is recommended to utilize 'graphql-tag' or an alternative approach to properly convert the parameter

Whenever I attempt to utilize useQuery from @apollo/client, the following error crops up: "Invariant Violation: Argument of [object Object] passed to parser was not a valid GraphQL DocumentNode. You may need to use 'graphql-tag' or another ...

Is there a way to access comprehensive data pertaining to an ID through Ajax?

I need help with an Ajax call. Below is the code I currently have: $.ajax({ async: true, url: 'test/', type: 'POST', datatype: 'text json', data: { id: id, }, success: function(data) { // Retrieve the da ...

Retrieve information using AJAX via POST method

I find myself in a bit of a pickle at the moment. I've been researching for hours, and I still can't seem to figure out this seemingly basic issue. It would be greatly appreciated if someone could offer me some quick advice. So here's my dil ...

Obtain email addresses from a Google account

I've encountered a challenge while creating a website for a client who requested the ability for users to sign up or log in using their Google and Facebook accounts. My current hurdle involves extracting the user's email address from their Google ...

Utilize axios-cache-interceptor to enforce caching of responses from axios

Is it possible to configure axios to always return a cached response with the help of axios-cache-interceptor? import axios from 'axios' import { setupCache } from 'axios-cache-interceptor' const axiosInstance = axios.create({ timeou ...

Tips for successfully transferring property values while utilizing Ajax in Struts 2

After configuring Struts 2 with Ajax, I encountered an issue where the form properties were not being passed to the action class, resulting in a value of null. This is my JSP: <head> <title>Login</title> <meta charset="ut ...

Problem with Node.js Sequelize ORM model

Experiencing an issue with my web application using Sequelize, the Node.js ORM. When starting the Node server, I encounter the following error: Error: project_directory_path\node_modules\sequelize\lib\sequelize.js:322 model.init(attr ...

Ways to enable asset sharing among various Angular.js applications on separate servers

Hello everyone, I am fairly new to the world of Angular.js and currently exploring its potential for our upcoming project. I am curious if anyone has experience with creating multiple Angular.js applications that are hosted on different servers (most li ...

Error message "Render function must return a valid value" occurs when utilizing buttons in mapped details

I am currently working on a project where I fetch data from the backend and display it on the frontend by mapping it out. Everything is functioning well, but when I try to add a button with some functionality (along with binding it and creating a handler) ...

Align Bootstrap navigation bar items at the center horizontally

I am currently working on a navigation bar that features two icons, evenly distributed. To achieve this look, I have been experimenting with scaling the icons horizontally in order to make them center-aligned within the navigation bar. However, I have not ...

Improving Code Efficiency in an Express Application by Extracting Duplicate Callback Functions

Hey there! I've been working with Node, Express, and Socket.IO, and I am looking to streamline my code by refactoring out redundant callbacks. Currently, my code looks like this: io.sockets.on('connection', function(socket) { socket.o ...

Why does Object.create accept a function as an argument in JavaScript?

Why is newperson4 successfully created instead of producing an error? See the code below: function person() { } var p = new person(); var q = null; var r = "some string"; var newperson1 = Object.create(p); //Runs without errors. var newperson2 = Objec ...

Mongoose - Modify and save changes using retrieved data

The current Model in use is as follows: const TestSchema: Schema = new Schema({ id: {type: String, required: true, unique: true}, array: [{ type: Number, required: true }], }, { timestamps: { createdAt: 'createdAt' }}); I require an endp ...

Setting the || operator with JSX in React can be done by using the logical OR

Hi, I'm trying to configure two conditional statements using React. Here is the code I have: <div> {item?.status === "One" || (item?.status === "Two" && ( <Button btn="primary" title="One text" /> ))} &l ...

There seems to be an issue with the sorting functionality in Mongoose

Why doesn't sorting work with Mongoose? I have searched extensively but still can't figure it out. Every time I attempt to implement it, it fails. Here is the code snippet in question: Pics.find({}).limit(8).populate('creator').sort("c ...