I have two document, how can we create lookup to get results? - mongodb

Here is an example of a type document:
{
id: 't-123'
attributes: [
{
id: 'a-1',
type: 'INPUT'
},
{
id: 'a-2',
type: 'SELECT',
option: [
{key: 'DE', label: 'Germany'},
{key: 'US', label: 'United States'}
]
}
]
}
Additionally, I have a form collection that references the type collection and stores all filled fields from a user.
Here is an example of a form document:
{
id: 'f-1',
type: 't-123',
values: [
{attribute: 'a-1', value: 'my random string'},
{attribute: 'a-2', value: 'DE'}
]
}
Now, I am trying to query the form collection and automatically add the choosen key/label object from the type collection for fields with type: SELECT.
Here is my desired result:
{
id: 'f-1',
type: 't-123',
values: [
{attribute: 'a-1', value: 'my random string'},
{attribute: 'a-2', value: {key:'DE', label:'Germany'}}
]
}

Related

MongoDB lookup to various collections based on a field value

Consider there is documents like this in DB:
{
name: 'my list',
items: [
{ type: 'book', id: 5364 },
{ type: 'car', id: 354 },
{ type: 'laptop', id: 228 }
]
}
I need to grab data of each item from its own collection, based on type value.
I searched about it but couldn't figure out the correct approach.
Expected output:
{
name: 'my list',
items: [
{ type: 'book', id: 5364, data: [{...}] },
{ type: 'car', id: 354, data: [{...}] },
{ type: 'laptop', id: 228, data: [{...}] }
]
}
Mongoose schema of first collection (above):
{
name: String,
items: {
type: Array,
default: []
}
}
And other collections that must be looked up has corresponding _id field.
There are a few different ways to do this. To be most similar to what you have currently, you just need to make one small change. type is a keyword in Mongoose, so if you want to have a field in your schema which is actually called "type", you need to use the word twice. Once for the Mongoose keyword and again to define your schema.
{
name: String,
items: [{
type: { type: String},
id: Number,
data: []
}]
}
If the data field is coming from another collection, you could use a reference and then call populate() on your find method.
// ItemSchema
{
name: String,
items: [{
type: { type: String },
id: Number,
data: [{
type: Schema.Types.ObjectId,
ref: 'OtherSchemaName'
}]
}]
}
// Find Method with populate
const myList = await ItemSchema.find({type: "book"}).populate('data')
// Result
{
name: 'my list',
items: [
{ type: 'book', id: 5364, data: [{...}] },
]
}

How to write MongoDB request?

Here is an example of a type document:
{
id: 't-123'
attributes: [
{
id: 'a-1',
type: 'INPUT'
},
{
id: 'a-2',
type: 'SELECT',
option: [
{key: 'DE', label: 'Germany'},
{key: 'US', label: 'United States'}
]
}
]
}
Additionally, I have a form collection that references the type collection and stores all filled fields from a user.
Here is an example of a form document:
{
id: 'f-1',
type: 't-123',
values: [
{attribute: 'a-1', value: 'my random string'},
{attribute: 'a-2', value: 'DE'}
]
}
Now, I am trying to query the form collection and automatically add the choosen key/label object from the type collection for fields with type: SELECT.
Here is my desired result:
{
id: 'f-1',
type: 't-123',
values: [
{attribute: 'a-1', value: 'my random string'},
{attribute: 'a-2', value: {key:'DE', label:'Germany'}}
]
}

MongoDB: How to get data from linked collection based on condition

I have a type collection that stores field informations. Each field has a type: Input, SelectBox, MultiSelect. Some field types have predefined options with key and label.
Here is an example of a type document:
{
id: 't-123'
attributes: [
{
id: 'a-1',
type: 'INPUT'
},
{
id: 'a-2',
type: 'SELECT',
option: [
{key: 'DE', label: 'Germany'},
{key: 'US', label: 'United States'}
]
}
]
}
Additionally, I have a form collection that references the type collection and stores all filled fields from a user.
Here is an example of a form document:
{
id: 'f-1',
type: 't-123',
values: [
{attribute: 'a-1', value: 'my random string'},
{attribute: 'a-2', value: 'DE'}
]
}
Now, I am trying to query the form collection and automatically add the choosen key/label object from the type collection for fields with type: SELECT. I am wondering if I can do this in mongoDB or only in node.js.
Here is my desired result:
{
id: 'f-1',
type: 't-123',
values: [
{attribute: 'a-1', value: 'my random string'},
{attribute: 'a-2', value: {key:'DE', label:'Germany'}}
]
}
So the query needs to check if the attribute.type is equal to 'SELECT', and find the related object from the options array, based on the key stored in the form. Else it takes the value stored in the form document.
Can I do anything like this within my MongoDB query or do I have to loop through my database results and manipulate the data manually ? THX!!

Mongodb schema for trello

I'm trying to make something similar to Trello.com. There will be many boards, and each board can have many lists, and each lists can have many items. The order of the lists and items can change often by the user, so I came up with something like this that would minimize queries. I just read some of the mongo documentation last night so I would appreciate any feedbacks and suggestions
Here is a list of boards:
{
boards: [
{
_id: 901292190
name: 'board1'
}
]
}
Here is when you view a single board:
{
_id: 901292190
name: 'board 1',
lists: [
{
_id: 932092,
name: 'todo',
items: [
{
_id: 9320903,
name: 'go shopping',
priority: 1,
createdby: 'user1',
assignedto: 'user2'
},
{
_id: 3902901,
name: 'go to school',
priority: 2,
createdby: 'user1',
assignedto: 'user2'
}
]
},
{
name: 'finished',
items: [
{
_id: 91209,
name: 'programming',
priority: 1,
createdby: 'user1',
assignedto: 'user2'
}
]
}
]
}

Chained Selectfields -- Sencha Touch 2.0

I am using the Sencha Touch 2.0 KitchenSink example to try to learn some of the basics. I am getting stuck on chained selectfields though.
I am going off the tutorial here but with the different setup I am being thrown off. The code below runs perfectly in the Forms.js file in kitchensink, but I am missing on the actual chainedselect part of it.
EDIT: What I am asking is how to make chained selectfields. I have the datastores and the selectfields in the example code, but not a working chained selectfield from first to second.
Ext.regModel('First', {
idProperty: 'FirstID',
fields: [{
name: 'FirstID',
type: 'int'
}, {
name: 'FirstName',
type: 'string'
}]
});
Ext.regModel('Second', {
idProperty: 'SecondID',
fields: [{
name: 'SecondID',
type: 'int'
},{
name: 'FirstID',
type: 'int'
}, {
name: 'SecondName',
type: 'string'
}]
});
var firstStore = new Ext.data.Store({
model: 'First',
data: [{
FirstID: 1,
FirstName: 'Kenworth'
}, {
FirstID: 2,
FirstName: 'Peterbilt'
}],
autoLoad: true
});
var secondStore = new Ext.data.Store({
model: 'First',
data: [{
SecondID: 1,
FirstID: 1,
SecondName: 'T800'
}, {
SecondID: 2,
FirstID: 1,
SecondName: 'T700'
}, {
SecondID: 3,
FirstID: 1,
SecondName: 'T660'
}, {
SecondID: 4,
FirstID: 1,
SecondName: 'T470'
}],
autoLoad: true
});
Ext.define('Kitchensink.view.Forms', {
extend: 'Ext.tab.Panel',
requires: [
'Ext.form.Panel',
'Ext.form.FieldSet',
'Ext.field.Number',
'Ext.field.Spinner',
'Ext.field.DatePicker',
'Ext.field.Select',
'Ext.field.Hidden'
],
config: {
activeItem: 0,
tabBar: {
// docked: 'bottom',
ui: 'dark',
layout: {
pack: 'center'
}
},
items: [
{
title: 'Basic',
xtype: 'formpanel',
id: 'basicform',
iconCls: 'refresh',
items: [
{
xtype: 'fieldset',
title: 'Enter Data',
instructions: 'Please enter the information above.',
defaults: {
labelWidth: '35%'
},
items: [
{
xtype: 'selectfield',
name: 'firstfield',
label: 'First',
store: firstStore,
displayField: 'FirstName',
valueField: 'FirstID'
}, {
xtype: 'selectfield',
name: 'secondfield',
label: 'Second',
store: secondStore,
displayField: 'SecondName',
valueField: 'SecondID'
}
]
}
]
}
]
},
onFirstChange: function(selectField, value){
var secondSelectField = this.items.get(1);
secondSelectField.store.clearFilter(); // remove the previous filter
// Apply the selected Country's ID as the new Filter
secondSelectField.store.filter('FirstID', value);
// Select the first City in the List if there is one, otherwise set the value to an empty string
var firstValue = secondSelectField.store.getAt(0);
if(firstValue){
secondSelectField.setValue(firstValue.data.SecondID);
} else {
secondSelectField.setValue('');
}
}
});