I want to retrieve a comment in a post to edit. I'm not sure how to approach this.
Here's what my Post document looks like:
{
"title" : "First Node.js App",
"body" : "testing 123",
"status" : "public",
"user" : "John Doe",
"date" : ISODate("2017-12-21T18:30:09.779Z"),
"comments" : [
{
"commentBody" : "This is awesome! ",
"commentUser" : ObjectId("5a3bfd5a9e65351f9c18ba18"),
"_id" : ObjectId("5a3c02379e65351f9c18ba1a"),
"commentDate" : ISODate("2017-12-21T18:49:27.620Z")
},
{
"commentBody" : "This is second comment.",
"commentUser" : ObjectId("5a3bfd5a9e65351f9c18gt19"),
"_id" : ObjectId("5a3c02379e65351f9c18ba1b"),
"commentDate" : ISODate("2017-12-21T18:49:27.620Z")
}
],
"allowComments" : true
}
How do I retrieve comment id ObjectId("5a3c02379e65351f9c18ba1a")?
I've tried the following but had no luck:
const post = await Post.findById(req.params.id);
const comment = post.comments.find({"_id": req.params.commentid});
You need to pass a callback to Array#find:
const comment = post.comments.find((el) => el._id === req.params.commentid);
Related
So if I have a tree structured as following in firebase then how can I fetch the unique key where the Name == "Employee 1" and this should return the key "LJrWJlgUPWUaPQx0ok09CswIkXg2" ?
"Users" : {
"LJrWJlgUPWUaPQx0ok09CswIkXg2" : {
"Shifts" : {
"dfaskjhfkashdf" : {
"endTime" : 1490907600,
"startTime" : 1490878800
}
},
"email" : "Employee1#gmail.com",
"employeeId" : "LJrWJlgUPWUaPQx0ok09CswIkXg2",
"fcmToken" : "eOVPmlxy9fc:APA91bEChCT-JwnH14yLrkWBdWBR-KJwj_FznOgwSzu-2JbL2hD5tRTl-7GkavLSihSzJMJ2_f7FjDhUgnU464EKNGaTUjoV0ZuLbJMlxtJeghhRCBDTbkm_J_yiH29IDXHVjmxbfVHw",
"name" : "Employee 1",
"password" : "Employee1",
"startDate" : "2017-01-23"
}
If I understand your question correctly, this ought to do the trick:
firebase.database().ref('Users')
.orderByChild('name')
.equalTo('Employee 1')
.limitToFirst(1)
.once('value', snap => {
let key = Object.keys(snap.val())[0]; // LJrWJlgUPWUaPQx0ok09CswIkXg2
let user = snap.child(key).val(); // the whole user object
});
I have a javascript file which includes the data in the following format.
index = {
"me" : {
"id" : "524bd089-2a7b-4a71-ba23-16354d6351ae",
"firstName" : "A=",
"lastName" : "T",
"pictureName" : "66s2c.jpg",
"username" : "a-t"
},
"spaces" : [ {
"org" : {
"id" : "524bd089-2a7b-4a71-ba23-16354d6351ae",
"firstName" : "An",
"lastName" : "Tuli",
"pictureName" : "66s2c.jpg",
"username" : "arli"
}
}, {
"user" : {
"id" : "60c4a171-172f-4f66-9014-8b4cf3e476e6",
"firstName" : "Ban",
"lastName" : "Idris",
"pictureName" : "../../../../default-pic/butterfly_200.png",
"username" : "banun-idris"
}
} ]
}
users["524bd089-2a7b-4a71-ba23-16354d6351ae"] = {
"id" : "524bd089-2a7b-4a71-ba23-16354d6351ae",
"firstName" : "A",
"lastName" : "T",
"pictureName" : "66s2c.jpg",
"username" : "a",
"libraries" : [ "lEy27AZavfSR", "l0yApAoo2l4b", "lJl22YOtacxY", "l0UhMCvrMmka", "lJMWpIoFnaK4", "lCZ9cYYjVJcv", "l8kynpyoaej7" ]
}
libraries["lEy27AZavfSR"] = {
"id" : "lEy27AZavfSR",
"name" : "My Main Library",
"description" : null,
"numKeeps" : 0,
"keeps" : [ ]
}
keeps["k4r5UIugqgfk"] = {
"id" : "k4r5UIugqgfk",
"keptAt" : 1449613295000,
"lastActivityAt" : 1449613295000,
"title" : "",
"url" : "",
"note" : null,
"tags" : [ ],
"libraries" : [ "lJl22YOtacxY" ],
"summary" : "",
"messages" : [ ]
}
I need to import data from this type of format into my mysql database.
I can't finalize and nail down the approach to do this
I tried to get the file content into a php file and then convert them into a array string but i get a null result.
$jsondata = file_get_contents('trial_data.js');
$data = json_decode($jsondata, true);
var_dump($data);
The second approach I think i can take is load the data in jquery file and send it using AJAX to a php file which adds it to the database.
Which approach do you think i should take and how to go about it.
Plus i need to figure out the ID from the first index array, so that I can get the ID from the me and org object for which i need to get the rest of the information
Since your data is not a valid JSON, you can use a regex to parse it, like this:
$file_content = file_get_contents('trial_data.js');
preg_match_all("/(\w+)\[\"([^\]]*)\"\]\s*\=\s*\{([^\}]*)\}/", $file_content, $matches);
foreach ($matches[1] as $key => $value) {
$data[$value][$matches[2][$key]] = json_decode('{'.$matches[3][$key].'}');
}
print_r($data);
//For example:
echo $data['users']['524bd089-2a7b-4a71-ba23-16354d6351ae']->pictureName;
//Output: 66s2c.jpg
Demo: https://3v4l.org/fW5gH
I am trying to get only the ObjectId's from One specific Document that is embedded in the projects Array.
Basically I am trying to make a database that will have users and each user will have there own projects.
Thank you !
db.users.find().pretty()
{
"_id" : ObjectId("5762c0cf2b9a78006373a684"),
"name" : "seq",
"pass" : "seq",
"projects" : [
{
"pid" : ObjectId("5762c0ba2b9a78006373a682"),
"name" : "aaa"
},
{
"pid" : ObjectId("5762c0ba2b9a78006373a683"),
"name" : "bbb"
}
]
}
{
"_id" : ObjectId("5762c28d2b9a78006373a687"),
"name" : "leq",
"pass" : "leq",
"projects" : [
{
"pid" : ObjectId("5762c2892b9a78006373a685"),
"name" : "ccc"
},
{
"pid" : ObjectId("5762c2892b9a78006373a686"),
"name" : "ddd"
}
]
}
let say we want two pids
{"pid" : ObjectId("5762c0ba2b9a78006373a682")} and
{"pid" : ObjectId("5762c2892b9a78006373a686"),}
and only inner documents
so required response should look like:
{
"_id" : ObjectId("5762c0ba2b9a78006373a682"),
"name" : "aaa"
},{
"_id" : ObjectId("5762c2892b9a78006373a686"),
"name" : "ddd"
}
Aggregation framework can manipulate documents, match only needed ones and transform inner structure by project phase:
var match = {
$match : {
"projects.pid" : {
$in : [ObjectId("5762c0ba2b9a78006373a682"),
ObjectId("5762c2892b9a78006373a686")]
}
}
}
var unwind = {
$unwind : "$projects"
};
// now move array objet as top level object
var project = {
$project : {
_id : "$projects.pid",
name : "$projects.name",
// list other fields here
}
}
db.vic.aggregate([match, unwind, match, project])
My mongoDB collection looks like this :
{
"_id" : ObjectId("5070310e0f3350482b00011d"),
"emails" : [
{
"_id" : ObjectId("5070310e0f3350482b000120"),
"_type" : "Email",
"name" : "work",
"email" : "peter.loescher#siemens.com",
"current" : true
}
]
}
and this is the .js code i use to print the contents :
c = db.contacts.findOne( { "emails.email" : { $ne : null } }, { "emails" : 1 } )
print(c._id.toString() + " " + c.emails[0]);
when I try to run this javascript file, it is just displaying the id but not the email array.
output:
5070310e0f3350482b00011d [object bson_object]
but when I try c.emails[0].email is is giving proper result. i.e. peter.loescher#siemens.com
All I need is I want to display the whole emails embedded object.
i.e.
"emails" : [
{
"_id" : ObjectId("5070310e0f3350482b000120"),
"_type" : "Email",
"name" : "work",
"email" : "peter.loescher#siemens.com",
"current" : true
}
]
Where I am going wrong?. Any help would be appreciated.
You need printjson to output a nicely formatted JSON:
printjson(c.emails[0]);
Here it is the documentation.
I am trying to delete "virtualNumber" : "12345" in the following document:
{
"_id" : ObjectId("50a9db5bdc7a04df06000005"),
"billingInfo" : null,
"date" : "dsfdsfsdfsd",
"description" : "sdfsdff",
"pbx" : {
"_id" : ObjectId("50a9db5bdc7a04df06000006"),
"did" : {
"1234567890" : {
"inventoryId" : "509df7547e84b25e18000001",
"didcountry" : "india",
"didState" : "bangalore",
"routeType" : "CallForward",
"didNumber" : "1234567890",
"didVirtualNumbers" : [
{
"virtualNumber" : "12345"
},
{
"virtualNumber" : "56789"
}
],
"id" : ObjectId("50a9db9acdfb4f9217000002")
}
},
},
I am using node.js, so I constructed a query in JavaScript:
var query = {_id: ObjectId("50a9db5bdc7a04df06000005")};
var obj = {};
obj["pbx.did.1234567890.didVirtualNumbers.virtualNumber"]=12345;
//problem
collection.update(query,{$pull:obj});
You need to match the array element like:
{"$pull": {"pbx.did.7259591220.didVirtualNumbers": {"virtualNumber": "12345"}}}
So you should change your code to:
obj["pbx.did.7259591220.didVirtualNumbers"]={"virtualNumber": "12345"};
Please refer to http://www.mongodb.org/display/DOCS/Updating#Updating-%24pull
It mentions the pull field should be an array.