Display image from Mongodb with NodeJS - node.js

I have a collection "image" and "image_ids "in my mongoDB:
image:
{
"_id" : ObjectId("542e65368a1cec1227ae2bac"),
"reference" : "5424991"
}
images_ids:
{
"_id" : ObjectId("542e652995ac12ed3fecb85f"),
"image" : "D0000001.PNG",
"fid" : ObjectId("542e65368a1cec1227ae2bac"),
"reference" : "5424991"
}
How I can be able to display D0000001.PNG with NodeJS? I mean, to do the link with and API.
[EDIT] we have also
fs.chunks:
{
"_id" : ObjectId("542e684a8a1cec178a172673"),
"n" : 1,
"data" : BinData(0,"2N6DSSfbCN/LLacNDYrJUQDEZgimMUwFpQGoJP0RU19Bi4PM82DjrUnKhE/P9v7P8ZveD1oDpFBM0iml9NE3WQmAvYFoG+nhD73Jm4N9b4LpylaAN5Ef+gVdgGSTAfSUwOikXoVick5pSQCkRmTCU5NT9VVfjHdAx74/ZhFRj+TIRjzlAhzkACBElzgMwGCo7tX+FYrpQLJ5KRmXiwFFwsNtHHzXiK1eu+CG1FumhGpA/qdG8CdDgD1xUHEcerMGO/eLGR9ML7ni/VjXxWzqp2j5DG2/WuKNv7xd3Kz/vr0MctJhuaBIl35YrKhdLnzqDa0uDa6bm4jz+eNyAI2hQbayGo4kVPFe8W7wFpY7qfBvnB9kbocxfZSdADDUNwYaydpT8lIcKEN9XfQJOYZvHp0El"),
"files_id" : ObjectId("542e684a8a1cec178a172671")
}
fs.files:
{
"_id" : ObjectId("542e65378axdeckhb0"),
"uploadDate" : ISODate("2012-11-01"),
"length" : 15673,
"chunkSize" : 33222,
"md5" : "f66e6654854a28e3672cfhds334d223b55a1"
}
Thanks for your help !

Related

$merge, $match and $update in one aggregate query

I have data in a collection ex:"jobs". I am trying to copy specific data from "jobs" after every 2 hours to a new collection (which may not exist initially) and also add a new key to the copied data.
I have been trying with this query to copy the data:
db.getCollection("jobs").aggregate([{ $match: { "job_name": "UploadFile", "created_datetime" : {"$gte":"2021-08-18 12:00:00"} } },{"$merge":{into: {coll : "reports"}}}])
But after this, the count in "reports" collection is 0. Also, how can I update the documents (with an extract key "report_name") without using an extra updateMany() query?
The data in jobs collection is as shown:
{
"_id" : ObjectId("60fa8e8283dc22799134dc6f"),
"job_id" : "408a5654-9a89-4c15-82b4-b0dc894b19d7",
"job_name" : "UploadFile",
"data" : {
"path" : "share://LOCALNAS/Screenshot from 2021-07-23 10-34-34.png",
"file_name" : "Screenshot from 2021-07-23 10-34-34.png",
"parent_path" : "share://LOCALNAS",
"size" : 97710,
"md5sum" : "",
"file_uid" : "c4411f10-a745-48d0-a55d-164707b7d6c2",
"version_id" : "c3dfd31a-80ba-4de0-9115-2d9b778bcf02",
"session_id" : "c4411f10-a745-48d0-a55d-164707b7d6c2",
"resource_name" : "Screenshot from 2021-07-23 10-34-34.png",
"metadata" : {
"metadata" : {
"description" : "",
"tag_ids" : [ ]
},
"category_id" : "60eed9ea33c690a0dfc89b41",
"custom_metadata" : [ ]
},
"upload_token" : "upload_token_c5043927484e",
"upload_url" : "/mnt/share_LOCALNAS",
"vfs_action_handler_id" : "91be4282a9ad5067642cdadb75278230",
"element_type" : "file"
},
"user_id" : "60f6c507d4ba6ee28aee5723",
"node_id" : "syeda",
"state" : "COMPLETED",
"priority" : 2,
"resource_name" : "Screenshot from 2021-07-23 10-34-34.png",
"group_id" : "upload_group_0babf8b7ce0b",
"status_info" : {
"progress" : 100,
"status_msg" : "Upload Completed."
},
"error_code" : "",
"error_message" : "",
"created_datetime" : ISODate("2021-07-23T15:10:18.506Z"),
"modified_datetime" : ISODate("2021-07-23T15:10:18.506Z"),
"schema_version" : "1.0.0",
}
Your $match stage contains a condition which takes created_datetime as string while in your sample data it is an ISODate. Such condtion won't return any document, try:
{
$match: {
"job_name": "UploadFile",
"created_datetime": {
"$gte": ISODate("2021-07-01T12:00:00.000Z")
}
}
}
Mongo Playground

MongoDB Manual and DBRef referencing using node js

I have two case ,first case: where in my key has reference to the same collection ,is this correct way to reference ?,if yes ! how to de-ref/link in node js ?,if not how to reference fom the same collection
{
"_id" : ObjectId("53402597d852426020000002"),
"address" : {
"$ref" : "home_adress"
},
"contact" : "987654321",
"dob" : "01-01-1991",
"name" : "Tom Benzamin",
"Post_address" : {
"$ref" : "home_adress"
},
"home_adress":"Street 1, NY"}
case two: reference to a different collection(DBRefs),How to detect the reference and send the second query to fetch the referenced value ?
{
"_id" : ObjectId("53402597d852426020000002"),
"address" : {
"$ref" : "address_home",
"$id" : ObjectId("534009e4d852427820000002"),
"$db" : "ref"
},
"contact" : "987654321",
"dob" : "01-01-1991",
"name" : "Tom Benzamin"}
{
"_id" : ObjectId("534009e4d852427820000002"),
"building" : "22 A, Indiana Apt",
"pincode" : 123456,
"city" : "Los Angeles",
"state" : "California"}

How to query data from mongodb collection by just the first 3 numbers

I have a collection in my mongodb that is called pnumber and looks like this when I do db.pnumber.find() mongo shell
{ "_id" : ObjectId("57696e03b9c614e1143c759e"), "pnumber" : NumberLong("5037816575") }
{ "_id" : ObjectId("57696e03b9c614e1143c759f"), "pnumber" : NumberLong("5087368074") }
{ "_id" : ObjectId("57696e03b9c614e1143c75a0"), "pnumber" : NumberLong("8055019263") }
{ "_id" : ObjectId("57696e03b9c614e1143c75a1"), "pnumber" : NumberLong("2153334813") }
{ "_id" : ObjectId("57696e03b9c614e1143c75a2"), "pnumber" : NumberLong("5592842086") }
{ "_id" : ObjectId("57696e03b9c614e1143c75a3"), "pnumber" : NumberLong("4356696916") }
{ "_id" : ObjectId("57696e03b9c614e1143c75a4"), "pnumber" : NumberLong("9545935012") }
{ "_id" : ObjectId("57696e03b9c614e1143c75a5"), "pnumber" : NumberLong("8083661362") }
{ "_id" : ObjectId("57696e03b9c614e1143c75a6"), "pnumber" : NumberLong("6065240853") }
{ "_id" : ObjectId("57696e03b9c614e1143c75a7"), "pnumber" : NumberLong("4236768328") }
{ "_id" : ObjectId("57696e03b9c614e1143c75a8"), "pnumber" : NumberLong("9705354839") }
I am using a mean stack application that is connected to this database through mongoose.
I would like to have an input field with a search button that when a user enters the area code that a list pops up from the collection of all the entry's that match that area code and have a download button that creates a csv with all of those entries.
I have:
var csv = require('express-csv');
in my app.js file.
Any info would be much appreciated.
db.pnumber.find( { pnumber: /^123/ } )
this results with all response that start with the value 123.
if you tie the form with this, the response should be the results you are looking for.
if you would like to match strings, use $regex operator
db.getCollection('blah').find({'phoneNumber':{'$regex': /^phoneNumber/}})
if you would like to match numbers, use $where operator.
db.getCollection('blah').find({$where: "/^970*/.test(this.phoneNumber)"})

mongodb descendent sorting by a numeric field getting sorted randomly (node.js)

I have a mongo database running in cmd.
There is a collection named usercollection.
And there is also some documents which I'm getting by typing in mongo shell:
db.usercollection.find().sort({score: -1}).pretty()
Output:
{
"_id" : ObjectId("57501b2d7ef17cd8c1bd63bc"),
"name" : "QWERTY",
"score" : "100",
"link" : "http://qwerty.com"
}
{
"_id" : ObjectId("5750199819433f80da88c847"),
"name" : "Adam Sandler",
"score" : "222",
"link" : "http://sandler.com"
}
{
"_id" : ObjectId("57501af57ef17cd8c1bd63bb"),
"name" : "gfhfghfghguy",
"score" : "5",
"link" : "http://dsad.com"
}
{
"_id" : ObjectId("57501bb47ef17cd8c1bd63bd"),
"name" : "Test name",
"score" : "50",
"link" : "http://fdsfds.cpm"
}
{
"_id" : ObjectId("574f4a9dab868fdc896d11b4"),
"name" : "Rich Man",
"score" : "500",
"link" : "http://jikl.me"
}
{
"_id" : ObjectId("574f50f1fbd256888ffa248d"),
"name" : "lfgfgff",
"score" : "9",
"link" : "http://oh.com"
}
{
"_id" : ObjectId("574f5324fbd256888ffa248e"),
"name" : "test name",
"score" : "9",
"link" : "http://dsad.com"
}
The problem is that I'm trying to sort these documents from one with highest score to one with lowest score and it's output is completely random(
I'm displaying that documents on the website using node.js and the result is the same as in the console.
Try with :
db.usercollection.find().sort({score: 'desc'}).pretty()

How to calculate using data from previous object and current object in mongodb array of objects using aggregate?

I have the following data in mongodb
{
"_id" : ObjectId("5641f77344aef29028a86d9b"),
"timestamp" : "2015-09-21 00:01:34",
"day_chan1" : NumberInt(0),
"day_chan2" : NumberInt(0),
"day_chan3" : NumberInt(65),
"gatwayId" : "4437191001a3"
}
{
"_id" : ObjectId("5641f77344aef29028a86d9c"),
"timestamp" : "2015-09-21 00:02:34",
"day_chan1" : NumberInt(0),
"day_chan2" : NumberInt(0),
"day_chan3" : NumberInt(97),
"gatwayId" : "4437191001a3"
}
{
"_id" : ObjectId("5641f77344aef29028a86d9d"),
"timestamp" : "2015-09-21 00:03:34",
"day_chan1" : NumberInt(0),
"day_chan2" : NumberInt(0),
"day_chan3" : NumberInt(130),
"gatwayId" : "4437191001a3"
}
and I want to calculate three extra fields ph1,ph2,ph3 from the given data where ph1 = day_chan1 of current - day_chan2 of previous object. Can this be done using aggregate in mongoose or pure mongodb ?

Resources