How may I create an array of JSON objects? - javascript

Each object represents a different user and each user has 3 properties: loginid, password, realname.
Below is what I have written. Is this cord correct?
var user= [
{"loginid":"email1#email.com", "password":1234, "realname":"A" };,
{"loginid":"email2#email.com", "password":1234, "realname":"B" };,
{"loginid":"email3#email.com", "password":1234, "realname":"C" };,
]

Like LGSon said, you need to drop the ; from each line. Also you can define field namaes without " ", like this:
var user= [
{loginid:"email1#email.com", password:1234, realname:"A" },
{loginid:"email2#email.com", password:1234, realname:"B" },
{loginid:"email3#email.com", password:1234, realname:"C" },
]

I believe this is what you want:
var user= [
{loginid:"email1#email.com", password:1234, realname:"A"},
{loginid:"email2#email.com", password:1234, realname:"B"},
{loginid:"email3#email.com", password:1234, realname:"C"},
];

that's how you can create array object JSON
{
"user": [{
"loginid": "email1#email.com",
"password": 1234,
"realname": "A"
},
{
"loginid": "email2#email.com",
"password": 1234,
"realname": "B"
},
{
"loginid": "email3#email.com",
"password": 1234,
"realname": "C"
}
]
}

It is correct, except the semicolons.
EDIT:
Actually, I think the correct way would be
{ "users": [
{"loginid":"email1#email.com", "password":1234, "realname":"A" },
{"loginid":"email2#email.com", "password":1234, "realname":"B" },
{"loginid":"email3#email.com", "password":1234, "realname":"C" },
]
}

Related

Possible to only query a sub document in Mongoose?

This is my test collection
[
{
"_id": "637cbf94b4741277c3b53c6c",
"text": "outter",
"username": "test1",
"address": [
{
"text": "inner",
"username": "test2",
"_id": "637cbf94b4741277c3b53c6e"
}
],
"__v": 0
}
]
If I do
t1 = await doc.find({}, 'text').exec();
console.log(JSON.stringify(t1, null, 2));
I get
[
{
"_id": "637cbf94b4741277c3b53c6c",
"text": "outter"
}
]
So here it finds the parent text.
Question
How do I get Mongoose to query the sub document instead of the parent?
You can project the sub document(s) like this:
t1 = await doc.find({}, {'address.text':1}).exec();
See how it works on the playground example
If you only want the sub document, not even the _id of the parent:
await doc.find(
{},
{
_id: 0,
'address.text': 1
}
).exec();`

How can i get an index of an Array in an Array of a Json Doc

I have a Json Doc which i get from my back end which looks like the one below. It has an emails Array which the doc under it has and email array. I need to get the index the email array is part of which has a match for an email address so i can update the doc. i used lodash before to search for a key in a array of docs before with _.find and _.indexOf but not sure how this would work in an Array of Array case.
{
"_type": "email_campaign",
"status": "Active",
"start_date": "04/17/2020",
"template_id": "template::0a2decdd-3acd-4661-9721-1a9ec0d039e6",
"summary": "Update",
"metrics": {
"first_email_sent": "",
"last_email_send": "2020-04-17T22:11:52.317Z",
"nbr_of_emails": 185,
"nbr_of_bounces": 1,
},
"history": {
"created_on": "04/17/2020 13:24:33",
},
"emails": [
{
"email": [
"DCustomer#gmail.com"
],
"track_request": "track_request::da07bd4b-b03e-4ecc-9d90-32e85cdb5b3a",
"tracking_nbr": "MD1dpjR6d"
},
{
"email": [
"ACustomer#matcocomponents.com",
"BCustomer#matcocomponents.com",
],
"track_request": "track_request::6f64cee1-d38e-4d68-94a2-c3c7d1287984",
"tracking_nbr": "YM1sMRX3nl"
},
{
"email": [
"ACustomer#gmail.com"
],
"track_request": "track_request::92606d4f-f9e6-457f-9156-167eb068f05b",
"tracking_nbr": "gj6dwIyHdE"
},
],
"_id": "3ed76589-4063-49f6-a21e-9ca16981d102"
}
You could use findIndex and includes:
const index = docs.emails.findIndex(item => item.email.includes('ACustomer#matcocomponents.com'))
try this:
emailToFind="xxxxxxxxx"
var indexes=[]
var test= res.emails.map((el1,i1)=>{
el.email.map((el2,i2)=>{
if (el2===emailToFind){
indexes.push(i1)
}
})
})

How can I merge two mongo collections?

I am banging my head against the wall on this...
SEE UPDATE 1 (below) !
I am merging two collections together... I looked at this example ( and ~several~ other examples here on SO ... )
https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/#lookup-single-equality
I think I am really close, but my expected results are not the same as what I would expect out of the example.
Here is the schema for 'Event'
const EventSchema = new Schema({
name: {type: String, required: true},
})
Here is some 'Event' data
[
{
"_id": "5e8e4fcf781d96df5c1f5358",
"name": "358 Event"
},
{
"_id": "5e8e55c5a0f5fc1431453b5f",
"name": "b5f Event"
}
]
Here is 'MyEvent' schema:
const MyEventSchema = new Schema({
userId: {type: Schema.Types.ObjectId, required: true},
eventId: {type: Schema.Types.ObjectId, required: true},
})
Here is some 'MyEvent' data
[
{
"_id": "5e8f4ed2ddab5e3d04ff30b3",
"userId": "5e6c2dddad72870c84f8476b",
"eventId": "5e8e4fcf781d96df5c1f5358",
}
]
Here is my code ( the code is wrapped in a promise so it returns resolve and reject with data )
var agg = [
{
$lookup:
{
from: "MyEvent",
localField: "_id",
foreignField: "eventId",
as: "userIds"
}
}
];
Event.aggregate(agg)
.then( events => {
return resolve(events);
})
.catch(err => {
return reject(null);
})
Here are my results,
[
{
"_id": "5e8e4fcf781d96df5c1f5358",
"name": "358 Event",
"__v": 0,
"UserIds": []
},
{
"_id": "5e8e55c5a0f5fc1431453b5f",
"name": "b5f Event",
"__v": 0,
"UserIds": []
}
]
I expect to see UserIds filled in for event '358 Event', like this
What am I missing ???
[
{
"_id": "5e8e4fcf781d96df5c1f5358",
"name": "358 Event",
"__v": 0,
"UserIds": [
{"userId": "5e6c2dddad72870c84f8476b"}
]
},
{
"_id": "5e8e55c5a0f5fc1431453b5f",
"name": "b5f Event",
"__v": 0,
"UserIds": []
}
]
UPDATE 1
I found a mongo playground and what I have works there, but it doesn't work in my code ??
https://mongoplayground.net/p/fy-GP_yx5j7
In case the link breaks, here is configuration: * select 'bson multiple collections'
db={
"collection": [
{
"_id": "5e8e4fcf781d96df5c1f5358",
"name": "358 Event"
},
{
"_id": "5e8e55c5a0f5fc1431453b5f",
"name": "b5f Event"
}
],
"other": [
{
"_id": "5e8f4ed2ddab5e3d04ff30b3",
"userId": "5e6c2dddad72870c84f8476b",
"eventId": "5e8e4fcf781d96df5c1f5358",
}
]
}
Here is Query:
db.collection.aggregate([
{
$lookup: {
from: "other",
localField: "_id",
foreignField: "eventId",
as: "userIds"
}
}
])
Here is the result:
[
{
"_id": "5e8e4fcf781d96df5c1f5358",
"name": "358 Event",
"userIds": [
{
"_id": "5e8f4ed2ddab5e3d04ff30b3",
"eventId": "5e8e4fcf781d96df5c1f5358",
"userId": "5e6c2dddad72870c84f8476b"
}
]
},
{
"_id": "5e8e55c5a0f5fc1431453b5f",
"name": "b5f Event",
"userIds": []
}
]
any suggestions as to why this doesn't work in my code... but works in the playground?
UPDATE 2
I found this:
Need a workaround for lookup of a string to objectID foreignField
UPDATE 3
I have changed the schema to use ObjectId for ids now
still doesn't work
And they are ObjectIds :
RESOLUTION:
So the real answer was a combination of Update 2 and Update 3 and using the right collection name in the lookup.
Update 2 is pretty much my very same question... just using different table names
Update 3 is the correct way to solve this issue.
Mohammed Yousry pointed out the collection name might be wrong... so I looked at my schema and I did have it wrong - changed the name to the right name (along with ObjectId types) and it worked !
It seems there's a typo in from property in $lookup, MyEvent maybe not the collection name
db.collection.aggregate([
{
$lookup: {
from: "MyEvent", // here is the issue I think, check the collection name and make sure that it matches the one you write here
localField: "_id",
foreignField: "eventId",
as: "userIds"
}
}
])
in mongo playground you attached in the question, if you change the 'other' in the $lookup to anything else, or make a typo in it .. like others instead of other, you will face the same issue
so check that there is no typo in the word MyEvent that you populate from

How to delete a paticular Object in an array inRethinkDB

I am Just trying to learn RethinkDB.I am little Bit Confused That how to delete an single Object in an array,What is the Exact query I have to use if i have to delete this Object
{
"name": "Ram" ,
"username": "B97bf210-c4d2d-11e6-b783-07b5fev048705"
}
from whoLikedIt Array
My data
{
"comments": [ ],
"id": "c242c74d-03d9-4963-9a22-4779facb8192" ,
.....
"views": 0 ,
"whoLikedIt": [
{
"name": "Vignesh Warar" ,
"username": "d97bf210-c42d-11e6-b783-07b5fe048705"
},
{
"name": "Ram" ,
"username": "B97bf210-c4d2d-11e6-b783-07b5fev048705"
},
]
.....
}
My Try
r.db('image').table('posts').get('c242c74d-03d9-4963-9a22-4779facb8192').update(
{whoLikedIt:r.row('whoLikedIt').filter({username:"B97bf210-c4d2d-11e6-b783-07b5fev048705"}).delete()}
)
Throws Me a error
e: Cannot nest writes or meta ops in stream operations. Use FOR_EACH instead in:
You want:
r.db('image').table('posts').get('c242c74d-03d9-4963-9a22-4779facb8192').update(function(row) {
return {whoLikedIt: row('whoLikedIt').filter(function(obj) {
return obj('username').ne("B97bf210-c4d2d-11e6-b783-07b5fev048705");
})};
})

Reading JSON in Javascript

I am trying to parse some json that my server replies me.
I am getting this answer from the server:
{
"ROWCOUNT": 1,
"COLUMNS": [
"REGISTRATION_DT",
"USERNAME",
"PASSWORD",
"FNAME",
"LNAME",
"EMAIL",
"MOBILE",
"FACEBOOK_ID"
],
"DATA": {
"REGISTRATION_DT": [
"March, 17 2012 16:18:00"
],
"USERNAME": [
"user"
],
"PASSWORD": [
pass
],
"FNAME": [
"name"
],
"LNAME": [
"lname"
],
"EMAIL": [
"somemail"
],
"MOBILE": [
mobile
],
"FACEBOOK_ID": [
"fbid"
]
}
}
I am trying to extract the data with this way:
var xml2 = this.responseData;
var xml3 = JSON.parse(xml2);
Ti.API.log(xml3.DATA[0].FNAME);
What I am doing wrong here?
You're reading your JSON wrong. DATA is an object of arrays and not vica versa.
Ti.API.log( xml3.DATA.FNAME[0] );
Ti.API.log(xml3.DATA.FNAME[0]);
Two fields come without quotes:
"PASSWORD": [
pass
]
And
"MOBILE": [
mobile
],
xml3.DATA is an object, not an array.
You need to write
xml3.DATA.FNAME[0]

Categories