0

I have a table called "files" wherein each object has a name, a parent, and some other fields. What I want to do is, given a filename, find the path to root (root occurs when parent is '').

I have a query that works perfectly when run in the Mongo terminal:

var currentFile = parent;
var queue = [];

while(currentFile !== '') {
    var file = db.files.find( { name: currentFile } );
    currentFile = file[0].parent;
    queue.push(currentFile);
}

Unfortunately, however, Mongoose does not allow (as far as I have seen from the documentation) arbitrary string queries. I could do it if Mongoose allowed for synchronous queries, but I cannot seem to find that ability either.

SOLVED

After re-reading the first comment, I found that to achieve what I needed, I needed to add a callback to the parameters that gets called when currentFile is empty

if(currentFile === '') {
    callback();
else
    recursiveFunc(currentFile, callback);

I also added the field of 'path' in case I decide to be a little bit more efficient :)

Thank you guys for all the help!

2 Answers 2

1

What about a recursive function?

var queue = [];
var currentFile = parent;
function findStuff(currentFile) {
    File.findOne({name: currentFile}, function(err, file) {
        currentFile = file[0].parent;
        queue.push(currentFile);
        findStuff(currentFile)
    })
}

Also as or "arbitrary query string", I'm not sure what you mean. It's standard mongo syntax. If you want to hit the node-mongodb-native driver directly you can always do something like this. It's still async though.... https://github.com/christkv/node-mongodb-native

File.collection.find({}, function(err, files) {});

What about something simpler? Sorry, I'm not fully understanding your intentions.

var queue = [];
var currentFile = parent;
File.find({name: currentFile}, function(err, files) {
    files.forEach(function(file) {
         queue.push(file.parent)
    })
})
Sign up to request clarification or add additional context in comments.

2 Comments

How could I call this and ensure that the array that I get after the call contains all of the elements? By "arbitrary query string," I mean send a string of BSON (with some JavaScript) to the MongoDB server, execute it, and get the results, a la MySQL.
ooooh. let me change it a little.
0

Perhaps you could avoid this entirely by storing an additional field in your documents that contains the path to root - say, an array of ObjectId's called path_to_root.

Then when inserting a new document, just copy the parent's value for path_to_root and append the parent's _id to it, and use this as path_to_root for the newly inserted document. With this strategy you could quickly query all the documents for the path back to root using a mongodb $in query.

Not sure if this could work for you without understanding the larger context, but just throwin it out there.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.