1

So I'm playing with MongoDb database, I've used so far with Python but now I'm trying to conquer Node.js. I've connected to mongo database with mongoose. Configured following schema:

var recipeSchema = new Schema({
    title: String,
    img: String,
    category: String,
    cook_time: String,
    method: String,
    person_count: String,
    short_desc:String,
    ingredients: [
        {
            amount: String,
            ingredient: String
        }
    ],
    recipe: String,
    advice: String

});
var Recipe = mongoose.model('Recipe', recipeSchema);

I have filled database with some autamation procedure in python, and the problem I have is that somehow I got the two unwanted charaters \n on beginning of my title string. I managed to find documents with Mongoose in Node, that has title item which are starting with \n with:

Recipe.find({ title: /\n/ }, 'title', function (err, document) {
    if (err) return handleError(err);
    console.log(document)
})

I'm little bit newbie in javascript so I'm gonna ask a question which is the best way to replace the \n with nothing (I suppose something like string.replace("\n","")) and update it back to my mongo database?

5
  • string.replace("\n", "") is exactly what you'd do. Did you try that? Commented Feb 14, 2014 at 18:00
  • I don't think .find({ title: /\n/ }, 'title',... is valid syntax. Also, I don't think Mongo has a find and replace strategy. I would use a cursor and use string.replace() as you have it. Commented Feb 14, 2014 at 18:02
  • it is valid, I'm getting back wanted docs in console.log. The problem is that I do not know procedure of replacing it and updating it back because im javascript newbie... Commented Feb 14, 2014 at 18:06
  • mongoosejs.com/docs/api.html#document_Document-update Commented Feb 14, 2014 at 18:30
  • can I update multiple documents at once with this one? Commented Feb 14, 2014 at 18:33

1 Answer 1

2

Suprisingly I think that I succeded with modification of my own code:

Recipe.find({ title: /\n/ }, 'title', function (err, document) {

    for (i = 0, max = document.length; i < max; i++) {
        console.log(document[i]);
        var newTitle = document[i].title.replace(/\n/,"");
        document[i].title = newTitle;
        document[i].save(function (err) {
            if(err) {
                console.error('ERROR!');
            }
        });
    }

});

I have looped through the found documents and then saved new stuff

Sign up to request clarification or add additional context in comments.

2 Comments

You might find that your documents are not updated here, because Mongoose.js restricts locks the documents that it returns. If that's happening on your end, you'll need to call each document's toJSON() method, make any modifications, and then save the JSON-ified document.
thanks for your advice, but that code worked for me as it was, documets are updated with new titles... the collection is not something complicated so far so I assume that because of it, this simple code worked for me

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.