How to delete a grid in extjs? - extjs4.2

I'm auto loading an Extjs grid for the first time, it loads with all the values from the servlet. The webpage also has a form based on which (user's input) another grid is being created. When the new grid is being created i don't want the old one. Or is there another way to load only the store? The code is below:
Ext.require([ 'Ext.grid.*', 'Ext.data.*', 'Ext.util.*', 'Ext.state.*',
'Ext.form.*',
'Ext.tip.QuickTipManager', 'Ext.ux.LiveSearchGridPanel' ]);
Ext.onReady(function() {
Ext.QuickTips.init();
var itemsPerPage = 50;
var simple = Ext.create('Ext.form.Panel', {
renderTo : 'form',
frame : true,
align : 'center',
title : 'Audit LOG',
bodyStyle : 'padding:5px 5px 0',
width : 825,
fieldDefaults : {
msgTarget : 'side',
labelWidth : 75
},
defaultType : 'textfield',
defaults : {
anchor : '100%'
},
items : [ {
fieldLabel : 'Date',
xtype : 'datefield',
name : 'Date',
id : 'Date',
format : 'd M Y',
allowBlank : false,
cls : 'input_single',
emptyText : 'dd-MMM-YYYY',
}, {
fieldLabel : 'Start Time',
name : 'sTime',
xtype : 'timefield',
id : 'sTime',
format : 'H:i:s',
altFormats : 'H:i:s'
}, {
fieldLabel : 'End Time',
name : 'eTime',
xtype : 'timefield',
id : 'eTime',
format : 'H:i:s',
altFormats : 'H:i:s'
} ],buttons : [ {
text : 'GO',
handler : function() {
var startDate = Ext.getCmp('Date').getValue();
var sTime = Ext.getCmp('sTime').getRawValue();
var eTime = Ext.getCmp('eTime').getRawValue();
grid(startDate,sTime,eTime);
}
} ]
});
var store = Ext.create('Ext.data.Store', {
id : 'pageStore',
autoLoad : false,
fields : [ {
name : 'S_NO',
type : 'int'
},{
name : 'LoggedTime',
type : 'string'
}, {
name : 'Level',
type : 'string'
}, {
name : 'LoggerName',
type : 'string'
}, {
name : 'MethodName',
type : 'string'
}, {
name : 'AuditMessage',
type : 'string'
}, {
name : 'Customer',
type : 'string'
}, {
name : 'Activity',
type : 'string'
} ],
pageSize : itemsPerPage,
proxy : {
type : 'ajax',
url : '/LogHandlers/Log',
params : {
flag:'no'
},
reader : {
type : 'json',
root : 'itemsList',
totalProperty : 'totalCount'
}
}
});
store.load({
params : {
start : 0,
limit : itemsPerPage,
flag:'no'
}
});
var groupingFeature = Ext
.create(
'Ext.grid.feature.Grouping',
{
groupHeaderTpl : '{columnName}:{groupValue} ({rows.length} Item{[values.rows.length > 1 ? "s" : ""]})',
hideGroupedHeader: true
});
var userGrid = new Ext.create('Ext.ux.LiveSearchGridPanel', {
renderTo : 'logs',
height : 600,
store : store,
features : [ groupingFeature
],
columns : [ {
header : 'S_NO',
dataIndex : 'S_NO',
items: [{
icon : "extjs/resources/ext-theme-classic/images/menu/menu-parent.gif" // Use a URL in the icon config
}]
},{
header : 'LoggedTime',
dataIndex : 'LoggedTime'
}, {
header : 'Level',
dataIndex : 'Level',
groupable : false,
}, {
header : 'LoggerName',
dataIndex : 'LoggerName',
groupable : false
}, {
header : 'MethodName',
dataIndex : 'MethodName',
groupable : false
}, {
header : 'AuditMessage',
dataIndex : 'AuditMessage',
groupable : false
}, {
header : 'Customer',
dataIndex : 'Customer'
}, {
header : 'Activity',
dataIndex : 'Activity'
} ],
dockedItems : [ {
xtype : 'pagingtoolbar',
pageSize : itemsPerPage,
store : store, // same store GridPanel is using
dock : 'bottom',
displayInfo : true,
text : 'Show Preview',
beforePageText : 'Page',
afterPageText : 'of {0}',
mode : 'local',
width : 50,
displayMsg : 'Displaying {0} - {1} of {2}',
emptyMsg : 'No Records to display'
}],
viewConfig : {
forceFit : true
}
});
});
function grid(startDate,sTime,eTime) {
var itemsPerPage = 50;
var store = Ext.create('Ext.data.Store', {
id : 'pageStore',
autoLoad : false,
fields : [ {
name : 'S_NO',
type : 'int'
},{
name : 'LoggedTime',
type : 'string'
}, {
name : 'Level',
type : 'string'
}, {
name : 'LoggerName',
type : 'string'
}, {
name : 'MethodName',
type : 'string'
}, {
name : 'AuditMessage',
type : 'string'
}, {
name : 'Customer',
type : 'string'
}, {
name : 'Activity',
type : 'string'
} ],
pageSize : itemsPerPage,
proxy : {
type : 'ajax',
url : '/LogHandlers/Log',
actionMethods: {
read: 'POST'
},
params : {
Date : startDate,
startTime : sTime,
endTime : eTime,
flag:'yes'
},
reader : {
type : 'json',
root : 'itemsList',
totalProperty : 'totalCount'
}
}
});
store.load({
params : {
start : 0,
limit : itemsPerPage,
Date : startDate,
startTime : sTime,
endTime : eTime,
flag:'yes'
}
});
var groupingFeature = Ext
.create(
'Ext.grid.feature.Grouping',
{
groupHeaderTpl : '{columnName}:{groupValue} ({rows.length} Item{[values.rows.length > 1 ? "s" : ""]})',
hideGroupedHeader: true
});
var userGrid = new Ext.create('Ext.ux.LiveSearchGridPanel', {
renderTo : 'logs',
height : 600,
store : store,
features : [ groupingFeature
],
columns : [ {
header : 'S_NO',
dataIndex : 'S_NO',
items: [{
icon : "extjs/resources/ext-theme-classic/images/menu/menu-parent.gif" // Use a URL in the icon config
}]
},{
header : 'LoggedTime',
dataIndex : 'LoggedTime'
}, {
header : 'Level',
dataIndex : 'Level',
groupable : false,
}, {
header : 'LoggerName',
dataIndex : 'LoggerName',
groupable : false
}, {
header : 'MethodName',
dataIndex : 'MethodName',
groupable : false
}, {
header : 'AuditMessage',
dataIndex : 'AuditMessage',
groupable : false
}, {
header : 'Customer',
dataIndex : 'Customer'
}, {
header : 'Activity',
dataIndex : 'Activity'
} ],
dockedItems : [ {
xtype : 'pagingtoolbar',
pageSize : itemsPerPage,
store : store, // same store GridPanel is using
dock : 'bottom',
displayInfo : true,
text : 'Show Preview',
beforePageText : 'Page',
afterPageText : 'of {0}',
mode : 'local',
width : 50,
displayMsg : 'Displaying {0} - {1} of {2}',
emptyMsg : 'No Records to display'
}],
viewConfig : {
forceFit : true
}
});
}

Are you saying you are trying to delete a grid, and recreate it, to pull the updated data from the server? If that is the case, just call load() on you store (with any params you want to pass in, like you are in your code), and that will load up-to-date data, same as when you created the store and grid. You can get the store from the grid by calling getStore(), so it would look something like this:
userGrid.getStore().load();

Related

MongoDB how to find documents by searching a Mixed Type field?

I am using the mongoose, and have a search text like Guo. I have following mongo document:
I want to find the text in the field "info.value"
{
"_id" : ObjectId("5aeefc3ce104b16c7e3337a2"),
...
"info" : [
{
"availbleNotInOrgs" : [],
"_id" : ObjectId("5b9e56dc09573f260c76af96"),
"field" : ObjectId("5b73d31ecebabf4b0d07a44c"),
"value" : "Guo"
},
{
"availbleNotInOrgs" : [],
"_id" : ObjectId("5b9e56dc09573f260c76af94"),
"field" : ObjectId("5b73d3a7cebabf4b0d07a48e"),
"value" : {
"id" : "a0",
"value" : "男"
}
},
{
"availbleNotInOrgs" : [],
"_id" : ObjectId("5b9e56dc09573f260c76af92"),
"field" : ObjectId("5b73d488cebabf4b0d07a4f9"),
"value" : [
7,
23
]
},
{
"availbleNotInOrgs" : [],
"_id" : ObjectId("5b9e56dc09573f260c76af90"),
"field" : ObjectId("5b73d50acebabf4b0d07a548"),
"value" : [
{
"country" : "中国",
"code" : "+86",
"phoneNumber" : "15699511079"
},
{
"country" : "中国",
"code" : "+86",
"phoneNumber" : "1841234123",
"placeholder" : "130xxxxxxxx"
}
]
}
]
}
The schema looks like:
const NewUserSchema = new Schema({
wechat: {
unionid: { type: String },
username: { type: String },
...
},
info: [{
field: { type: mongoose.Schema.Types.ObjectId, ref: 'field' },
value: Schema.Types.Mixed,
availbleNotInOrgs: [{ type: mongoose.Schema.Types.ObjectId, ref: 'organization' }]
}],
});
The type of "info.value" is Schema.Types.Mixed
so that value could be an array, an object, a string...
For instance, I expect to match the following documents that info.value contains Guo
{
"info" : [
{
"value" : [
{
"country" : "Guo",
"phoneNumber" : "15699511079"
},
{
"code" : "+86",
"phoneNumber" : "15699511079"
}
]
}
]
}
{
"info" : [
{
"value" : [
{
"code" : "+86",
"users" : ["15699511079","Guo"]
}
]
}
]
}
What can I do if I keep the type of "info.value"?

MongoDB Console - Find & pull object in an array based on nested object with regex [duplicate]

This question already has an answer here:
Updating/Pulling a value in an Array contained in a Subdocument Array in Mongodb/Mongoose
(1 answer)
Closed 3 years ago.
I have run into a problem where I need to delete some entries in my database based on a regexp that looks for an 'x' char in the value of the field. This is my data structure:
{
"_id" : ObjectId("5e0effac04d45483d97cb4c3"),
"formId" : ObjectId("5d076f7db166bcae2743554a"),
"revisionNo" : 0,
"customerUserId" : ObjectId("5d0889fde55fbc38509e5825"),
"formElements" : [
{
"formElementId" : ObjectId("5d1a047cae7a711848238c26"),
"type" : "text",
"label" : "Intersection number",
"value" : null,
"dynamicTypeData" : {
"required" : false,
"value" : "TK47ax"
}
},
{
"formElementId" : ObjectId("5d1a058cae7a711848238c27"),
"type" : "select",
"label" : "Municipality",
"value" : null,
"dynamicTypeData" : {
"attributes" : null,
"options" : [
],
"required" : false
}
},
{
"formElementId" : ObjectId("5d00ec9a9001eb637c8466f4"),
"type" : "select",
"label" : "Signtype for setup",
"value" : null,
"dynamicTypeData" : {
"attributes" : null,
"options" : [
],
"required" : false
}
},
{
"formElementId" : ObjectId("5d00ee749001eb637c8466fc"),
"type" : "select",
"label" : "Sign size",
"value" : null,
"dynamicTypeData" : {
"attributes" : null,
"options" : [
],
"required" : false
}
},
{
"formElementId" : ObjectId("5d00eced9001eb637c8466f5"),
"type" : "select",
"label" : "Route number",
"value" : null,
"dynamicTypeData" : {
"attributes" : null,
"options" : [
],
"required" : false
}
},
{
"formElementId" : ObjectId("5d00eeba9001eb637c8466fe"),
"type" : "select",
"label" : "Stand",
"value" : null,
"dynamicTypeData" : {
"attributes" : null,
"options" : [
],
"required" : false
}
},
{
"formElementId" : ObjectId("5d00ee849001eb637c8466fd"),
"type" : "select",
"label" : "Foundation",
"value" : null,
"dynamicTypeData" : {
"attributes" : null,
"options" : [
],
"required" : false
}
},
{
"formElementId" : ObjectId("5d00ed0f9001eb637c8466f6"),
"type" : "textarea",
"label" : "Remarks regaring the new signs placement",
"value" : null,
"dynamicTypeData" : {
"value" : null,
"required" : false
}
}
]
}
So what I want to find is every element matches the label: "Intersection number" with a value that contains the char x in their dynamicTypeData object. I'm aware that I can in the console do something like: db.getCollection('formRegistrations').find({formId: ObjectId("5d0889fde55fbc38509e5825")}) but how to get all the way down to thedynamicTypeData` object and match with the value in that, I have no idea. Please Help :) Thanks for reading
EDIT
While the given answer is correct I still only wanted to find the given registrations with those parameters, because I had to manually review the data before deletion since this was done on a production ser. So from the answer given below I formulated my own script:
db.formRegistrations.find(
{
formId: ObjectId("5d076f7db166bcae2743554a"),
"formElements.0.label": "Intersection number",
"formElements.0.dynamicTypeData.value": {
$regex: /.*x/
}
}
)
Hope this helps someone other than myself
To remove the formElements matching a specific label and dynamicTypeData(child elements) value, this should work :
db.formRegistrations.update({ _id: <the id> }, {
$pull: {
formElements: {
"label": "Intersection number",
"dynamicTypeData.value": {
$regex: /^.*?x.*$/,
$options: "i"
}
}
}
})

MongoDB update field only if cursor has field

I am updating MongoDB document using a cursor loop. But in the below query cursor owner field cannot exist in the results. I am using below query but looks like results.hasOwnProperty('owner') always returning true and it is throwing error results.owner doesn't exists.
Even I tried db.contact_coolection.find({"_id" : ObjectId("5b876144d87b4d06ecb571b8")}) --> This gives ownerCRMContactId.$cond is not valid.
I think $cond is aggregator operator, what should be used in this case?
db.contact_coolection.find().forEach(function(results)
{    
    print( "Id: " + results._id);
    db.contact_coolection.update( {_id : results._id},
{$set : {
"ownerCRMContactId": {
$cond: { if: results.hasOwnProperty('owner'), then: results.owner.$id, else: '' }
}
}
});
});
Below is the sample document doesn't containing owner
{
"_id" : ObjectId("5b876144d87b4d06ecb571b8"),
"_class" : "com.cfcf.crm.model.auth.CRMContact",
"crmId" : -09898,
"prefix" : "Mr",
"firstName" : "fghh",
"middleName" : "asdgasd",
"lastName" : "asdasd",
"suffix" : "asdassd",
"nickName" : "asdasd",
"gender" : "Male",
"age" : "0",
"dlNumber" : "0",
"height" : 0,
"weight" : 0,
"isSmoker" : false,
"deleted" : false,
"isEnabled" : false,
"externalSource" : [],
"familyMembers" : [],
"crmInfos" : [
{
"opportunityId" : "5b95fa6c28e76b60c0454a2e"
}
],
"createdDate" : ISODate("2018-08-30T03:15:16.181Z"),
"lastModifiedDate" : ISODate("2018-09-10T05:00:28.627Z"),
"createdBy" : "5b2f43e433d58d3e0cd15304",
"lastModifiedBy" : "5b2f43e433d58d3e0cd15304",
"owner" : {
"$ref" : "contact_coolection",
"$id" : ObjectId("5b2f43e433d58d3e0cd15303")
},
"roles" : [],
"links" : [
{
"linkId" : 0,
"linkTitle" : "testsLinkss",
"linkUrl" : "www.google.com"
}
],
"addresses" : [
{
"addressLine1" : "3rd Cross",
"addressLine2" : "Kensington Street",
"city" : "Birmingham",
"state" : "Alabama",
"country" : "US"
}
]
}
You can do this way
const promises = db.contact_coolection.find().forEach(async(results) => {
print( "Id: " + results._id);
await db.contact_coolection.update(
{ _id: results._id },
{ $set: {
ownerCRMContactId: results.owner ? results.owner.$id : ''
}}
)
})
await Promise.all(promises)
Update
const promises = db.contact_coolection.find().forEach(async(results) => {
print( "Id: " + results._id);
await db.contact_coolection.update(
{ _id: results._id },
{ $set: {
ownerCRMContactId: (results && results.owner) ? results.owner.$id : ''
}}
)
})
await Promise.all(promises)

MongoDB 3.2 Update Field name in embedded array

Im trying to do a i migration in my MongoDB. I have updated a Field from Content -> StringContent. Now I want to update all records that exists with the new Field name.
This is how a document looks like.
{
"_id" : "c4af0b19-4c78-4e58-bbe5-ac9e5cce2c3f",
"Type" : "Onboarding",
"Cards" : [
{
"_id" : LUUID("3f328a1c-658d-ee4e-8f06-561760eb5be5"),
"Width" : 1,
"Title" : "",
"Type" : "Freetext",
"Description" : "",
"Content" : "This is a test" // -> "StringContent" : "This is a Test"
},
{
"_id" : LUUID("2f328a1c-658d-ee4e-8f06-561760eb5be5"),
"Width" : 1,
"Title" : "",
"Type" : "Freetext",
"Description" : "",
"Content" : "This is another test" //-> "StringContent" : "This is another Test"
}
],
"DocumentType" : "Template",
"History" : [
{
"Date" : ISODate("2017-07-13T12:03:01.620Z"),
"ByUserId" : LUUID("4ecaa6ca-2ce6-f84c-81f3-28f8f0256e6e")
}
],
"Name" : "Default Template"
}
I have created this script:
var bulk = db.getCollection('OnboardingPortal').initializeOrderedBulkOp(),
count = 0;
db.getCollection('OnboardingPortal').find({"DocumentType": "Template"}).
forEach(function(doc) {
doc.Cards.forEach(function(card) {
if(card.hasOwnProperty("Content")){
print(card);
bulk.find({"_id": doc._id, "Cards._id": card._id}).update(
{
$set: {"Cards.$.StringContent": card.Content}
});
bulk.find({"_id": doc._id, "Cards._id": card._id}).update(
{
$unset: {"Cards.$.Content": 1}
});
count += 2;
if(count % 500 == 0) {
bulk.execute();
bulk = db.getCollection('OnboardingPortal').initializeOrderedBulkOp();
}
}
});
});
if ( count % 500 !== 0 ){
bulk.execute();
}
This Does not update anything, but if I change bulk.operations -> to explicit set index on array like this, it will do the job. but only for one card :
bulk.find({"_id": doc._id, "Cards.1._id": card._id}).update(
{
$set: {"Cards.1.StringContent": card.Content}
});
bulk.find({"_id": doc._id, "Cards.1._id": card._id}).update(
{
$unset: {"Cards.1.Content": 1}
});
What am i missing in my script so this can iterates over several documents and change Content-> StringContent in each Card. ?
EDIT
I have added a bulk.getOperations(); in my script. this is what it returns. Should it not have replaced $ with index ?
/* 1 */
[
{
"originalZeroIndex" : 0.0,
"batchType" : 2.0,
"operations" : [
{
"q" : {
"_id" : "c4af0b19-4c78-4e58-bbe5-ac9e5cce2c3f",
"Cards._id" : "1c8a323f-8d65-4eee-8f06-561760eb5be5"
},
"u" : {
"$set" : {
"Cards.$.StringContent" : "This is a cool test"
}
},
"multi" : false,
"upsert" : false
},
{
"q" : {
"_id" : "c4af0b19-4c78-4e58-bbe5-ac9e5cce2c3f",
"Cards._id" : "1c8a323f-8d65-4eee-8f06-561760eb5be5"
},
"u" : {
"$unset" : {
"Cards.$.Content" : 1.0
}
},
"multi" : false,
"upsert" : false
},
{
"q" : {
"_id" : "c4af0b19-4c78-4e58-bbe5-ac9e5cce2c3f",
"Cards._id" : "1c8a322f-8d65-4eee-8f06-561760eb5be5"
},
"u" : {
"$set" : {
"Cards.$.StringContent" : "This is a test"
}
},
"multi" : false,
"upsert" : false
},
{
"q" : {
"_id" : "c4af0b19-4c78-4e58-bbe5-ac9e5cce2c3f",
"Cards._id" : "1c8a322f-8d65-4eee-8f06-561760eb5be5"
},
"u" : {
"$unset" : {
"Cards.$.Content" : 1.0
}
},
"multi" : false,
"upsert" : false
}
]
}
]

Add new element to Mongo Document

I want to insert array element where Title is 'Cities' but it either doesnt insert or inserts in new array not Links.Link. Any Idea what I am doing wrong. I have tried push and $addToSet
{
"Component" : [
{
"Title" : "regions",
"Links" : {
"Link" : [
{
"Text" : "Tyrol",
"Url" : "/1xf-en700p/cheap-holidays-tyrol",
"Title" : "Cheap holidays Tyrol"
},
{
"Text" : "Browse Regions ",
"Url" : "/1xf-en6ujz-10ts/cheap-holidays-austria/regions",
"Title" : "Cheap holidays Austria",
"Style" : "BrowseForMore"
}
]
}
},
{
"Title" : "cities",
"Links" : {
"Link" : [
{
"Text" : "Maria Saal",
"Url" : "/1xf-enu4dl/cheap-holidays-maria-saal",
"Title" : "Cheap holidays Maria Saal"
},
{
"Text" : "Nessendorf",
"Url" : "/1xf-enwvu8/cheap-holidays-nessendorf",
"Title" : "Cheap holidays Nessendorf"
},
]
}
},
],
"Id" : "125570",
}
with
var pd = { Text: 'test',Url: 'test',Title: 'test' };
db.PopularDestinationsUk.update
(
{
Id: '125570'
,'Component.Title': 'cities'}
}
,
{
$push:
{
'Component.Link.$.Links { Text: 'NEW',Url: 'NEW',Title: 'NEW' }}}
}
}
)
but it doesnt insert into right place
Update your query as follows :
db.PopularDestinationsUk.update({Id: "125570", "Component.Title" : "cities"}, {$push : {"Component.$.Links.Link" : {Text: 'NEW',Url: 'NEW',Title: 'NEW'}}})