As a preface, I'm using Node.js with Mongo-db-native.
I'm also using GridFS to store images and each image has meta data, one of which is a Product Id.
I want to query all the the fs.files and return images that are associated to a specific product.
Here's how I am currently doing this:
this.collection.ensureIndex({
product_id: 1,
}, function (err, edIndex) {
self.collection.group( ['group'] , {
"product_id": ObjectID(product_id)
} , {
docs: []
} , function (doc, prev) {
prev.docs.push({
width: doc.width,
height: doc.height,
_id: doc._id
});
} , true , function (err, results) {
if (err) {
callback(err)
} else {
callback(null, results)
}
});
});
I'm finding that this is extremely slow. Does anyone have any suggestions as an alternative or how to increase the performance of this?
Thank you!
Here is a simple query in Mongo shell syntax which will find all GridFS files with ProductId = 42 in its metadata.
db.fs.files.find({"metadata.ProductId": 42});
The returned documents will contain a filename which can be used with mongo-native's GridStore.
Related
I have a MongoDB database with documents representing locations on a map, the document structure is below.
I'am trying to query for documents with sw_x and sw_y value that is 1000 more or 1000 less than the value of the user location which i get from another post request.
This is my get request:
router.get('/getdata', (req, res) =>{
mongoose.connect(url, function(err, db) {
if (err) throw err;
db.collection("mArGo").find({}).toArray(function(err, result) {
if (err) throw err;
console.log(result);
db.close();
});
})
})
Currently this returns all documents in the database, i just need to figure out how to filter them.
So in other words i need my query to return docs with sw_x, sw_y values that are greater than or less than the user location value by 1000
DB document example:
{
_id: new ObjectId("6172a1dcb5ce25759cd9506b"),
epsg: 25832,
filename: 'dom_657000_5354000_500.sfb',
offset_x: -650000,
offset_y: -5360000,
size_x: 500,
size_y: 500,
x_sw: 657000,
y_sw: 5354000
}
In the code, you can calculate min, max of x_sw and y_sw and pass it to query:
db.collection.find({
x_sw: { $gt: 656000, $lt: 658000 },
y_sw: { $gt: 5353000, $lt: 5355000 }
})
I have a JSON document in DocumentDB.
I wish to add new data to a given document.
For example.
Current document:
{
"User": "ABC",
"UserID": "123"
}
New document
{
"User": "ABC",
"Height":"1.60",
"UserID": "123"
}
I have tried solutions such as mentioned here:
Add new properties to DocumentDB Document in Stored procedure
But I get "getContext is not defined".
I believe the issue is that I am unsure of how to call a document via a function.
I would like:
var documentURL = "dbs/'dbname'/colls/'collsname'/docs/'docsname'"
editDocument(documentURL)
function editDocument(document) {
var collection = getContext().getCollection();
var response = getContext().getResponse();
document.Height = "1.60";
collection.createDocument(collection.getSelfLink(), document, function(err, result) {
if(err) throw err;
// Return the resulting document back as the response.
response.setBody(result);
});
}
Should I use client.replaceDocument instead, or maybe there is a better way to do this.
The issue may be that the function is plainly trying to use the text location "dbs/colls/docs" in the function rather than the document itself.
Thanks for any help in advance!
Yes, you need to use replaceDocument instead.
var documentURL = "dbs/'dbname'/colls/'collsname'/docs/'docid'"
client.readDocument(documentURL, (err, doc) => {
if(err) return console.log(err);
doc.Height = "1.60";
client.replaceDocument(documentUrl, doc, (err, result) => {
if(err) return console.log(err);
console.log('replaced document');
});
});
I have an application developed in NodeJS, which works as a REST API and consumes data from MongoDB
In MongoDB I have a collection called 'ftp' with more than 10 million documents with the following structure
{
"_id" : ObjectId("59e7c66911506bd1725cf145"),
"ip" : "72.32.177.76",
"timestamp" : "2017-10-16T02:30:26-04:00",
"data" : {
"banner" : "220-FileZilla Server version 0.9.41 beta\r\n"
}
}
The "data.banner" field is a hased index
From NoodeJs I make an aggregate query that filters a string of text using a regular expression, groups and counts the results.
function getData(timeInit, table, textSearch, res) {
MongoClient.connect(url, function (err, db) {
if (err) throw err;
db.collection(table).aggregate([
{
$match: { 'data.banner': $regex:textSearch}
}, {
$group: {
_id: '$data.banner',
num: { $sum: 1 },
}
},
{
$sort: {
num: -1
}
},{
$limit:5
}
], {
allowDiskUse: true
}
).toArray(function (err, result) {
if (err) throw err;
var timeFinal = new Date();
var data = {
result: result,
timeLapse: (timeFinal - timeInit) / 1000,
numResult: result.length
};
res.send(data);
db.close();
});
});
};
The query with regular expression takes about 8 seconds to return results, an excessive time in my opinion, since the regular expressions are not optimal.
My question is how should I make the filter to search for documents that contain text in an optimal way reducing the response time.
If someone knows how to optimize this type of query I would appreciate it a lot.
I'm trying to implement an update method for our API and I'm kinda new to the Node so I didn't know what would be the best practice to carry out the task of updating some fields of a document. Let me elaborate, we have a user model which keeps basic info of a user like name, age, sex, school, bio, birthday etc. Our update method should work as this, the request of the method includes the new values of the fields provided such as {bio:'newBio'} or {school:'newSchool', name:'newName'} I must update the provided fields with the provided data and leave the rest as they are. I was wondering what the best approach to the problem at hand would be. Thanks in advance
The easiest approach what i can think of is to use $Set to perform the update operations.
an example would be :
var updatedUsers= function(db, callback) {
db.collection('users').updateMany(
{ "_id": "value"},
{
$set: { bio: "new bio" }
}
,
function(err, results) {
console.log(results);
callback();
});
};
and invoke your above function as :
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
updatedUsers(db, function() {
db.close();
});
});
I have been working with mongoose for a while and I am currently stuck with this problem:
I have a dynamic MongoDB database on which a an express end-point batch inserts records using Model.collection.insert. The insertion is perfect, I can see all the documents by query using mongo shell and the document count is as expected.
Here is format of the documents that are inserted:
[
{
"disk":"Aff90",
"graph":
{
"Q1":{
"frequency":21
},
"Q2":{
"frequency":13
},
"Q3":{
"frequency":35
},
"Q4":{
"frequency":24
}
},
"sales": 987,
"deep_discount": 50,
"demand_score": 3.4
},
{
"disk":"B721",
"graph":
{
"Q1":{
"frequency":98
},
"Q2":{
"frequency":129
},
"Q3":{
"frequency":812
},
"Q4":{
"frequency":240
}
},
"sales": 2017,
"deep_discount": 80,
"demand_score": 7.4
},//and so on(about 1000 documents)
]
However, when I use my express end-point to query back that data, it only returns the first element. I have not encountered this particular problem with mongoose yet and am unable to find a solution on the web.
Express API code for the retrieval:
router.post('/end-point', function(req, res, next) {
model.find({}, {"_id":0}, function(err, data){
if(err){
throw err;
}
if(data.length){
res.json(data);
}
});
});
Can someone help me understand what the issue is about and how I could solve it?