I have a data structure that looks like this:
addresses: [
{
type: 'home',
street: 'street',
city: 'city',
zipcode: 'zpipcode',
},
{
type: 'work',
street: 'street',
city: 'city',
zipcode: 'zpipcode',
}
]
I want to model it like this
addresses: {
home: {
street: 'street',
city: 'city',
zipcode: 'zpipcode',
},
work: {
street: 'street',
city: 'city',
zipcode: 'zpipcode',
}
}
I am using now GenericEmbeddedDocumentField but no luck so far.
I found a solution. Have a look at below code. Remember to import mongoengine classes mentioned in the example (Document, MapField, etc..)
Example of model(s) definition
class Address(EmbeddedDocument):
street = StringField(required=True, max_length=256)
zipcode = StringField(required=True, max_length=16)
city = StringField(required=True, max_length=32)
country = StringField(default="Poland")
class User(Document):
email = EmailField(required=True)
addresses = MapField(field=EmbeddedDocumentField(Address))
Example of usage
user = User(email='test#mail.com')
user.addresses['home'] = Address(street='street', zipcode='zip', city='city')
user.save()
Related
Is there a simple way to return filtered items based on most matches.
For example if I have the following items in my DB:
Customer 1:
{
name: 'Customer 1',
street: 'Examplestreet 1',
}
Customer 2:
{
name: 'Name',
street: 'Examplestreet 2',
}
and then I'd search for name: "Name" and street: "Example".
I'd like to get the following results, because the first customer has two matches one in name and one in street and the second customer only has a match in street:
[
{
name: 'Name',
street: 'Examplestreet 2',
},
{
name: 'Customer 1',
street: 'Examplestreet 1',
}
]
Is there an easy way to do this using NestJS TypeORM Querybuilder?
Thanks
const login = await Signupdetails.find(req.body);
login =
[
{
PersonalInformation: {
firstName: 'aravind',
lastName: 'john',
phoneNumber: 9736363777,
DOB: 1999-07-08T18:30:00.000Z
},
Address: {
street: 'pillai',
city: 'Chennai',
pincode: 89997,
state: 'kerala',
country: 'India'
},
password: 'social',
_id: 607804f65c166c32144ae227,
email: 'hari#gmail.com',
userName: 'hari',
__v: 0
}
]
If I try using login.userName ,its showing undefined . This is a part of my Get API request . I used node.js and mongodb
You have a list, if you want login to be an Object, remove '[' characters:
login =
{
PersonalInformation: {
firstName: 'aravind',
lastName: 'john',
phoneNumber: 9736363777,
DOB: 1999-07-08T18:30:00.000Z
},
Address: {
street: 'pillai',
city: 'Chennai',
pincode: 89997,
state: 'kerala',
country: 'India'
},
password: 'social',
_id: 607804f65c166c32144ae227,
email: 'hari#gmail.com',
userName: 'hari',
__v: 0
}
On the other hand, if you want login to be a list, you have to access:
login[0].userName
I try to use a node function in firebase callable function and wehn I try to use it as described I see this warning on VS code. any help? on what I can do
module "c:/Users/firat/Desktop/vuedg3/functions/node_modules/iyzipay/lib/Iyzipay"
Could not find a declaration file for module 'iyzipay'. 'c:/Users/firat/Desktop/vuedg3/functions/node_modules/iyzipay/lib/Iyzipay.js' implicitly has an 'any' type.
Try npm install #types/iyzipay if it exists or add a new declaration (.d.ts) file containing declare module 'iyzipay';ts(7016)
and this callable function return null when I try and call it from web app
this is my entire code
exports.trypay = functions.https.onCall((data, context) => {
var Iyzipay = require('iyzipay');
var iyzipay = new Iyzipay({
apiKey: "sandbox-11",
secretKey: "sandbox-11",
uri: 'https://sandbox-api.iyzipay.com'
});
var request = {
locale: Iyzipay.LOCALE.TR,
conversationId: '123456789',
price: '1',
paidPrice: '1.2',
currency: Iyzipay.CURRENCY.TRY,
installment: '1',
basketId: 'B67832',
paymentChannel: Iyzipay.PAYMENT_CHANNEL.WEB,
paymentGroup: Iyzipay.PAYMENT_GROUP.LISTING,
paymentCard: {
cardHolderName: 'John Doe',
cardNumber: '5528790000000008',
expireMonth: '12',
expireYear: '2030',
cvc: '123',
registerCard: '0'
},
buyer: {
id: 'BY789',
name: 'John',
surname: 'Doe',
gsmNumber: '+905350000000',
email: 'email#email.com',
identityNumber: '74300864791',
lastLoginDate: '2015-10-05 12:43:35',
registrationDate: '2013-04-21 15:12:09',
registrationAddress: 'Nidakule Göztepe, Merdivenköy Mah. Bora Sok. No:1',
ip: '85.34.78.112',
city: 'Istanbul',
country: 'Turkey',
zipCode: '34732'
},
shippingAddress: {
contactName: 'Jane Doe',
city: 'Istanbul',
country: 'Turkey',
address: 'Nidakule Göztepe, Merdivenköy Mah. Bora Sok. No:1',
zipCode: '34742'
},
billingAddress: {
contactName: 'Jane Doe',
city: 'Istanbul',
country: 'Turkey',
address: 'Nidakule Göztepe, Merdivenköy Mah. Bora Sok. No:1',
zipCode: '34742'
},
basketItems: [
{
id: 'BI101',
name: 'Binocular',
category1: 'Collectibles',
category2: 'Accessories',
itemType: Iyzipay.BASKET_ITEM_TYPE.PHYSICAL,
price: '0.3'
},
{
id: 'BI102',
name: 'Game code',
category1: 'Game',
category2: 'Online Game Items',
itemType: Iyzipay.BASKET_ITEM_TYPE.VIRTUAL,
price: '0.5'
},
{
id: 'BI103',
name: 'Usb',
category1: 'Electronics',
category2: 'Usb / Cable',
itemType: Iyzipay.BASKET_ITEM_TYPE.PHYSICAL,
price: '0.2'
}
]
};
iyzipay.payment.create(request, function (err, result) {
return('result:'+result + 'error:'+err);
});
})
I am unable to solve this issue (and looking to avoid loop update one by one), please hep me out here.
I have a fambio document (which has its own FamilyModel) which gets created after user gives his below information:
{
name: 'john',
lname: 'doe',
}
Now, after above information gets saved, user provides more information about the family after some processing in backend:
let familyArr = [
{ _id: 1234, name: 'Jenny', lname: 'doe', relation: 'mother' },
{ _id: 2345, name: 'Jawn', lname: 'doe', relation: 'father' },
{ _id: 3456, name: 'Jane', lname: 'doe', relation: 'sister' },
{ _id: 4567, name: 'Daisy', lname: 'wick', relation: 'pupper' }
]
Altogether, FamilyModel schema looks like:
const FamilyModel = mongoose.Schema({
name: {type: String, required: true},
lname: {type: String, required: true},
family: [relationshipSchema]
}, {
timestamp: true
});
const relationshipSchema = mongoose.Schema({
name: {type: String, required: true},
lname: {type: String, required: true},
relation: {type: String, required: true}
}, {
required: false,
timestamp: true
});
Now, John has an array of objects of family (filed type Array) and trying to insert that Array of Object like this:
What I tried multiple options:
db.fambio.updateOne({_id: 1111}, { $set: { family: familyArr }})
db.fambio.findOneAndUpdate({_id: 1111}, { $push: { family: familyArr }});
db.fambio.update({_id: 1111}, $addToSet: { 'family': familyArr}});
Nothing is working with respect to insert the constructed Object directly to the field. When I insert one at a time, it gets updated.
How am I supposed to write the query to update/append an Array of Objects in to a field of Array Type having its own Schema maintained.
Okay, I've solved it. How I solved it:
Initially, I saved the document and did some processing on the info as per my requirements post which I wanted to insert Array of Elements in to an Array key field where I was running in to the issue of nothing getting inserted. What I was missing was $each and this is how I did it:
db.getCollection('fambio').update({
_id: johnsuserid
}, {
$push: {
'family': {
$each:[
{ _id: 1234, name: 'Jenny', lname: 'doe', relation: 'mother' },
{ _id: 2345, name: 'Jawn', lname: 'doe', relation: 'father' },
{ _id: 3456, name: 'Jane', lname: 'doe', relation: 'sister' },
{ _id: 4567, name: 'Daisy', lname: 'wick', relation: 'pupper' }
]
}
}
});
Thank you everyone!! Hope this helps someone who may face this problem in future.
I've got a query that is coming back with cartesians because one of the table joins can have many results. The use case is I have a company that can have many locations.
Here's the response I'm getting in my query:
[
{
// this is the company info
oid: 31,
companyName: 'Foo Bar Inc',
avatar: 'http://placehold.it/100x100',
bannerImage: 'http://placehold.it/800x200',
// here starts the location
name: 'Foo Bar Port',
address: '1234 West 31 Street',
city: 'New York',
state: 'New York'
},
{
oid: 31,
companyName: 'Foo Bar Inc',
avatar: 'http://placehold.it/100x100',
bannerImage: 'http://placehold.it/800x200',
name: 'Foo Bar Warehouse',
address: '644 Main Street',
city: 'Los Angeles',
state: 'California'
}
]
I would eventually like to get this to turn into:
{
oid: 31,
companyName: 'Foo Bar Inc',
avatar: 'http://placehold.it/100x100',
bannerImage: 'http://placehold.it/800x200',
locations: [
{
name: 'Foo Bar Port',
address: '1234 West 31 Street',
city: 'New York',
state: 'New York'
},
{
name: 'Foo Bar Warehouse',
address: '644 Main Street',
city: 'Los Angeles',
state: 'California'
}
]
}
What is the best way to handle this?
This below example if you use Sequelize ORM in node.js:
Here Company and Location is model.
// Company.js
// ...
Company.associate = function(models) {
// Using additional options like CASCADE etc for demonstration
// Can also simply do Company.belongsTo(models.Location);
Company.belongsTo(models.Location, {
onDelete: "CASCADE",
foreignKey: {
allowNull: false
}
});
}
// ...