Topic
  • 3 replies
  • Latest Post - ‏2013-01-23T18:50:18Z by SystemAdmin
SystemAdmin
SystemAdmin
2327 Posts

Pinned topic JSONStore.remove() removes multiple docs when given a Document.

‏2013-01-22T20:32:07Z |
The documentation for JSONStore.remove() says that the first argument can be a Document or Array of Documents or Query or Integer.

If I call JSONStore.remove() with a query as the first argument, all is well.

If I call JSONStore.remove() with an array of Documents (containing a single document) it removes a whole mess of Documents.

For example: I call JSONStore with the first argument being an array containing just:

{"_id":1,"json":{"LASTNAME":"ADAMSON","WORKDEPT":"D11","MIDINIT":" ","EMPNO":"000150","JOB":"DESIGNER","FIRSTNME":"BRUCE","PHONENO":"4510"}}

and the following get removed from the collection:
{"_dirty":"1358885627.92291","_id":1,"_operation":"remove","json":{"LASTNAME":"ADAMSON","WORKDEPT":"D11","MIDINIT":" ","EMPNO":"000150","JOB":"DESIGNER","FIRSTNME":"BRUCE","PHONENO":"4510"}},
{"_dirty":"1358885627.93947","_id":10,"_operation":"remove","json":{"LASTNAME":"LEE","WORKDEPT":"E21","MIDINIT":" ","EMPNO":"000330","JOB":"FIELDREP","FIRSTNME":"WING","PHONENO":"2103"}},
{"_dirty":"1358885627.94078","_id":11,"_operation":"remove","json":{"LASTNAME":"LUTZ","WORKDEPT":"D11","MIDINIT":"K","EMPNO":"000220","JOB":"DESIGNER","FIRSTNME":"JENNIFER","PHONENO":"0672"}},
{"_dirty":"1358885627.94205","_id":12,"_operation":"remove","json":{"LASTNAME":"MEHTA","WORKDEPT":"E21","MIDINIT":"V","EMPNO":"000320","JOB":"FIELDREP","FIRSTNME":"RAMLAL","PHONENO":"9990"}},
{"_dirty":"1358885627.94313","_id":13,"_operation":"remove","json":{"LASTNAME":"NICHOLLS","WORKDEPT":"C01","MIDINIT":"A","EMPNO":"000140","JOB":"ANALYST ","FIRSTNME":"HEATHER","PHONENO":"1793"}},
{"_dirty":"1358885627.94427","_id":14,"_operation":"remove","json":{"LASTNAME":"PEREZ","WORKDEPT":"D21","MIDINIT":"L","EMPNO":"000270","JOB":"CLERK ","FIRSTNME":"MARIA","PHONENO":"9001"}},
{"_dirty":"1358885627.94554","_id":15,"_operation":"remove","json":{"LASTNAME":"PIANKA","WORKDEPT":"D11","MIDINIT":"R","EMPNO":"000160","JOB":"DESIGNER","FIRSTNME":"ELIZABETH","PHONENO":"3782"}},
{"_dirty":"1358885627.94684","_id":16,"_operation":"remove","json":{"LASTNAME":"PULASKI","WORKDEPT":"D21","MIDINIT":"D","EMPNO":"000070","JOB":"MANAGER ","FIRSTNME":"EVA","PHONENO":"7831"}},
{"_dirty":"1358885627.94825","_id":17,"_operation":"remove","json":{"LASTNAME":"QUINTANA","WORKDEPT":"C01","MIDINIT":"M","EMPNO":"000130","JOB":"ANALYST ","FIRSTNME":"DELORES","PHONENO":"4578"}},
{"_dirty":"1358885627.94956","_id":18,"_operation":"remove","json":{"LASTNAME":"SMITH","WORKDEPT":"D21","MIDINIT":"S","EMPNO":"000250","JOB":"CLERK ","FIRSTNME":"DANIEL","PHONENO":"0961"}},
{"_dirty":"1358885627.95139","_id":19,"_operation":"remove","json":{"LASTNAME":"SPENSER","WORKDEPT":"E21","MIDINIT":"Q","EMPNO":"000100","JOB":"MANAGER ","FIRSTNME":"THEODORE","PHONENO":"0972"}},
{"_dirty":"1358885627.9527","_id":21,"_operation":"remove","json":{"LASTNAME":"THOMPSON","WORKDEPT":"B01","MIDINIT":"L","EMPNO":"000020","JOB":"MANAGER ","FIRSTNME":"MICHAEL","PHONENO":"3476"}}
It seems to be doing a wildcard match on the _id of the provided Document rather than removing exactly the Document provided.
  • SystemAdmin
    SystemAdmin
    2327 Posts

    Re: JSONStore.remove() removes multiple docs when given a Document.

    ‏2013-01-23T04:59:09Z  
    Unfortunately you are correct, a bug has been opened and we have started working on the issue.

    Here's a work around:

    • Try to make sure every document in the collection is unique. You can make sure of that by including a unique id (like EMPNO in your example) provided by the back-end.

    • Call remove and use the json blob as the query, for example:

    
    collection.remove(
    {
    "LASTNAME":
    "ADAMSON",
    "WORKDEPT":
    "D11",
    "MIDINIT":
    " ",
    "EMPNO":
    "000150",
    "JOB":
    "DESIGNER",
    "FIRSTNME":
    "BRUCE",
    "PHONENO":
    "4510"
    });
    


    That should only remove the document with _id == 1 in your collection, I assume some combination of those fields make the document unique.

    If you need to remove more than 1 document add a loop.

    • Here's an example, the goal is to remove just the first document:

    
    var collection = WL.JSONStore.initCollection(
    'collection2', 
    {fn: 
    'string', age: 
    'number'
    }, 
    { onSuccess: function () 
    { collection.store([
    {fn: 
    'carlos', age: 99
    }, 
    {fn: 
    'tim', age: 88
    }, 
    {fn: 
    'carlos', age: 98
    }], 
    {onSuccess: function () 
    { collection.remove(
    {fn: 
    'carlos', age: 99
    }, 
    { onSuccess: function (count) 
    { console.log(
    'Removed ' + count + 
    ' document(s)'); collection.findAll(
    { onSuccess: function (results) 
    { console.log(JSON.stringify(results)); 
    } 
    });
    //end findAll 
    }
    });
    //end remove 
    }
    });
    //end store 
    }
    //end init onSuccess 
    });
    //end init
    


    The output:
    
    Removed 1 document(s) [
    {
    "_id":2,
    "json":
    {
    "age":88,
    "fn":
    "tim"
    }
    },
    {
    "_id":3,
    "json":
    {
    "age":98,
    "fn":
    "carlos"
    }
    }]
    
  • SystemAdmin
    SystemAdmin
    2327 Posts

    Re: JSONStore.remove() removes multiple docs when given a Document.

    ‏2013-01-23T14:38:03Z  
    Unfortunately you are correct, a bug has been opened and we have started working on the issue.

    Here's a work around:

    • Try to make sure every document in the collection is unique. You can make sure of that by including a unique id (like EMPNO in your example) provided by the back-end.

    • Call remove and use the json blob as the query, for example:

    <pre class="jive-pre"> collection.remove( { "LASTNAME": "ADAMSON", "WORKDEPT": "D11", "MIDINIT": " ", "EMPNO": "000150", "JOB": "DESIGNER", "FIRSTNME": "BRUCE", "PHONENO": "4510" }); </pre>

    That should only remove the document with _id == 1 in your collection, I assume some combination of those fields make the document unique.

    If you need to remove more than 1 document add a loop.

    • Here's an example, the goal is to remove just the first document:

    <pre class="jive-pre"> var collection = WL.JSONStore.initCollection( 'collection2', {fn: 'string', age: 'number' }, { onSuccess: function () { collection.store([ {fn: 'carlos', age: 99 }, {fn: 'tim', age: 88 }, {fn: 'carlos', age: 98 }], {onSuccess: function () { collection.remove( {fn: 'carlos', age: 99 }, { onSuccess: function (count) { console.log( 'Removed ' + count + ' document(s)'); collection.findAll( { onSuccess: function (results) { console.log(JSON.stringify(results)); } }); //end findAll } }); //end remove } }); //end store } //end init onSuccess }); //end init </pre>

    The output:
    <pre class="jive-pre"> Removed 1 document(s) [ { "_id":2, "json": { "age":88, "fn": "tim" } }, { "_id":3, "json": { "age":98, "fn": "carlos" } }] </pre>
    In my example, it is easy to work around because every document has a unique 6 digit ID: EMPNO

    However, if EMPNO was defined just as a unique number, instead of as a unique 6 digit number, I would be in trouble.

    When I called find({EMPNO: "1"}) to get the document with EMPNO="1", I would also get the documents with EMPNO="10", "11", "21", "100"... Just to find the JSON BLOB to use as the query for my remove() I would need to implement my own search() function to iterate over the collection contents and check the EMPNO of each document until I find the correct one.

    The way JSONStore queries in general work (as a case insensitive substring match) is really powerful. But there are times you would wish it had the ability to query for an exact match.
  • SystemAdmin
    SystemAdmin
    2327 Posts

    Re: JSONStore.remove() removes multiple docs when given a Document.

    ‏2013-01-23T18:50:18Z  
    In my example, it is easy to work around because every document has a unique 6 digit ID: EMPNO

    However, if EMPNO was defined just as a unique number, instead of as a unique 6 digit number, I would be in trouble.

    When I called find({EMPNO: "1"}) to get the document with EMPNO="1", I would also get the documents with EMPNO="10", "11", "21", "100"... Just to find the JSON BLOB to use as the query for my remove() I would need to implement my own search() function to iterate over the collection contents and check the EMPNO of each document until I find the correct one.

    The way JSONStore queries in general work (as a case insensitive substring match) is really powerful. But there are times you would wish it had the ability to query for an exact match.
    You are correct, that's the current behavior. I can't go into specifics about unreleased features, but for what it's worth we will work to improve it.