Deleting an array of documents from the JSON store in IBM Worklight

I am developing an IBM Worklight hybrid app and utilizing JSON store to manage data storage. I have successfully implemented a delete function to remove records from the collection using the record's id. However, I am now seeking assistance on how to delete multiple records at once from the JSON store. If anyone has an example or can provide guidance on this matter, I would greatly appreciate it. Thank you in advance.

Delete Function:

var id = JSON.parse(localStorage.getItem('jsonindex'));
var query = {
    _id: id
};
var options = {
    push: true
};
try {
    WL.JSONStore.get(PEOPLE_COLLECTION_NAME).remove(query, options)
        .then(function (res) {
        console.log("REMOVE_MSG");
    })
        .fail(function (errorObject) {
        console.log("Not Removed");
    });
} catch (e) {
    alert(INIT_FIRST_MSG);
}

JSON Data

[{
    "_id": 16,
    "json": {
        "name": " Debit",
        "cardmonth": " 8",
        "cardyear": " 2028",
        "number": " 4216170916239547"
    }
}, {
    "_id": 17,
    "json": {
        "name": " Credit",
        "cardmonth": " 7",
        "cardyear": " 2027",
        "number": " 4216170916239547"
    }
}]

Answer №1

Give it a shot:

WL.JSONStore.get('collectionName').remove([{_id: 1}, {_id: 2}], options);

Instead of ..., insert {_id: 1}, {_id: 2} or any query you prefer for deleting documents.

If this method doesn't yield results, make sure to update to the newest version of Worklight and try once more.

Additional information:

  • PI10959: JSONSTORE FAILS TO REMOVE ALL DOCS IN THE DOC ARRAY WHEN A DOC ARRAY IS PASSED
  • IBM Worklight JSONStore | Remove Document from Collection and erase it from memory

Answer №2

Deleting a single record is simple, but deleting multiple records can lead to performance issues when dealing with a large number of records.

var id="7"; When you delete one record using the Delete method, you can easily apply the same method for multiple records.

var ids=[];
When a user selects an item, push the item's id into the ids array.

for(i=0;i<ids.length;i++){
Delete(ids[i]); //this is your custom Delete method
}

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

injecting the href into an iframe, but directing it to the parent window

Here are the code snippets I am working with: <html> <head> <title>Iframe</title> </head> <body> <a href="somepage.html" target="content">SomePage</a><br/> <a href="anotherpa ...

Locate the nearest date (using JavaScript)

Searching for the nearest date from an XML file. <?xml version="1.0" encoding="UTF-8"?> <schedule> <layout fromdt="2014-01-01 00:00:00" todt="2014-01-01 05:30:00"/> <layout fromdt="2014-02-01 00:00:00" todt="2014-01-01 05 ...

IsContainer and IsModel properties in Dragular are not functioning properly with the accept or canBeAccepted methods

Scenario 1 : Let's consider using two containers, named A (Drag Source) and B (Drop Source). Code snippet : dragularService(containerLeft, { containersModel: [DragularconTainer], copy: true, canBeAccepted: function(el, source) { ...

The client_id from Facebook oauth is not functioning properly to obtain the access token needed to retrieve feeds

I'm currently utilizing the https://graph.facebook.com/oauth/access_token?client_id={app_id}6&client_secret={app's_secret}&grant_type=client_credentials in order to obtain a token that allows me to access and read the feeds from a spec ...

Calculating value using 4 radio buttons with varying values in JavaScript

I'm facing an issue with calculating values using JavaScript. Here's my user interface: https://i.sstatic.net/TjPAG.png Scenario: When I click on the buttons - None, Weak, Moderate, or anything else, they correspond to values 0, 1, 2, and 3 resp ...

Performing an asynchronous request within a dynamic helper (or a suitable substitute) in Express version 2.x

I'm attempting to display the total message count for a user's inbox on my website's layout. I initially thought of utilizing Express' dynamicHelpers to achieve this, but in versions of Express <= 2.x, these helpers do not support as ...

Why do I keep receiving the error message "Cannot set property of undefined"?

In the midst of a small project that involves utilizing nuxt, js, and axios, I encountered an issue when attempting to assign the response data to my formFields object. Despite having declared formFields within the data section, I kept receiving an error m ...

Managing WebDriver for Selenium tests in JavaScript

As someone who is new to writing Selenium tests in Javascript, I have been wondering if there is a Driver Manager similar to WebDriverManager that can be used in JS tests. Despite my extensive search efforts, I have come up empty-handed so far. It seems ...

What methods can I use to enable web browsers to remember form field data that is not transmitted in the usual manner?

The code provided here is almost perfect in terms of functionality. function post_form() { http=new XMLHttpRequest(); http.onreadystatechange=function() { if (http.readyState==4 && http.status==200) some_div.innerHTML=http.responseText; ...

Preserve the parameter navigation stored within a flatlist and showcase it on a separate screen

I am looking to save a navigation parameter from a flatlist so that it can be displayed on another screen. This parameter is essentially a title stored in an array for the flatlist. react-native ScreenA <FlatList data={ this.state.FlatListItems } key ...

Leveraging React useEffect for retrieving data and managing component behavior

Having trouble querying data, setting it to state with useEffect, and rendering the results? I've tried a few things but only see changes when state is updated. Any advice would be greatly appreciated. Thank you in advance. useEffect // fetchCamp ...

Requirements detailed in package.json

Suppose we have a client-side application (such as an Ember app). We define the package.json file for our application with its dependencies listed. { name: "my-app", dependencies: { "dep1" : "1.0.0" }, devDependencies: { ...

spill the elements from one div into another div

I'm facing a situation where I have 2 divs on a page, with the first div containing text content only. The issue is that when the content of the first div overflows, it gets cut off due to the CSS applied to it: .one { overflow: hidden; width: 1 ...

Opt for a submit <button> over an <input> button option

Is it possible to submit a form using <button> instead of <input>? I understand that a <button> cannot handle the form submission like a <input>, and I need to use the onclick method. I have attempted various solutions, but none se ...

Are there advantages to incorporating d3.js through npm in an Angular 2 TypeScript project?

Summary: It is recommended to install d3.js via npm along with the typings. The accepted answer provides more details on this issue. This question was asked during my initial stages of learning Angular. The npm process is commonly used for tree-shaking, pa ...

What is the best method to redirect users who are not logged in from every URL when using PassportJs?

I am currently developing a web application using NodeJS, and I have integrated PassportJS with passport-local-mongoose for authentication. Below is the code snippet I've created for the root route to verify if the user is logged in: app.get('/ ...

Discovering inner arrays within an outer array using the Plus function

Apologies if these questions seem basic in terms of JavaScript. Challenge 1. I am attempting to write code that can identify an inner array within an outer array, and the structure of the array looks something like this; var array = ['Bob', [ ...

Using an array inside a for loop to follow the same structure

I am in need of prompt assistance on how to integrate my array into a for loop. Currently, my graph accepts the following as nodes: var classes = [ {"name":"test.cluster1.item1"}, {"name":"test.cluster1.item2"}, {"name":"test.cluster1.item3"} ...

Encountering an issue with React Native where initializing a project with npx leads to a "

Running the command: npx react-native init MyApp An error occurred while downloading and copying the template. Error message: Cannot find module 'C:\Users\%%%%\AppData\Local\Temp\rncli-init-template-rVvcjE\node_ ...

Why is my manifest.json acting strange?

I am in the process of integrating a manifest.json file into my PHP-based web application so that users can easily add it to their home screens. Below is the content of my manifest json: { "name": "OrganizaMeuCasamento", "short_name": "MeuCasamento", ...