Skip to content

Instantly share code, notes, and snippets.

@hcientist
Created February 16, 2015 21:43
Show Gist options
  • Save hcientist/3a9c309d728278c0b60d to your computer and use it in GitHub Desktop.
Save hcientist/3a9c309d728278c0b60d to your computer and use it in GitHub Desktop.
working with recipes in mongo
var r = { //first i will show an example where i just store the value of the recipe key, because there were no other keys in the example you sent me
"publisher": "All Recipes",
"f2f_url": "http://food2fork.com/view/521",
"ingredients": [
"For Cupcakes:",
"1 (18.25 ounce) package white cake mix with pudding",
"1/3 cup vegetable oil",
"3 egg whites",
"1 1/4 cups water",
"6 ounces bittersweet chocolate, chopped fine",
" ",
"For Filling:",
"1 cup cold heavy whipping cream",
"2 tablespoons confectioners' sugar",
"1/2 cup frozen unsweetened raspberries",
"1/4 cup chocolate-coated toffee bits",
"1/2 cup finely chopped toasted hazelnuts, skins removed",
" ",
"For Ganache:",
"1/2 cup heavy cream",
"6 ounces semisweet chocolate, chopped",
" ",
"For Frosting:",
"1 (12 ounce) package white chocolate chips",
"1 cup unsalted butter, at room temperature",
"2 (8 ounce) packages cream cheese, softened",
"2 teaspoons vanilla extract",
"food coloring, if desired (optional)"
],
"source_url": "http://allrecipes.com/Recipe/Zuccotto-Cupcakes/Detail.aspx",
"recipe_id": "521",
"image_url": "http://static.food2fork.com/8942993716.jpg",
"social_rank": 80.98230224538719,
"publisher_url": "http://allrecipes.com",
"title": ""Zuccotto" Cupcakes"
};
db.recipes.save(r); // store the obj in the db
db.recipes.find({"publisher": "All Recipes"}); // look for where publisher is exatcly so
db.recipes.find({publisher: "All Recipes"}); // nice i don't need quotes when the key has no funky characters
db.recipes.find({publisher: /all/i}); // now i want to do a partial search on the publisher, the last i means case insensitive
db.recipes.find({ingredients:{$regex: /egg/i}});// find recipes where at least one of the ingredients has the (case insensitive) string "egg" (even in the middle of a word)
db.recipes.find({ingredients: /egg/i}); // nice, don't have to write that weird regex thing
var r2 = { // ex2 where i use the whole pile of JSON you gave me.
"recipe": {
"publisher": "All Recipes",
"f2f_url": "http://food2fork.com/view/521",
"ingredients": [
"For Cupcakes:",
"1 (18.25 ounce) package white cake mix with pudding",
"1/3 cup vegetable oil",
"3 egg whites",
"1 1/4 cups water",
"6 ounces bittersweet chocolate, chopped fine",
" ",
"For Filling:",
"1 cup cold heavy whipping cream",
"2 tablespoons confectioners' sugar",
"1/2 cup frozen unsweetened raspberries",
"1/4 cup chocolate-coated toffee bits",
"1/2 cup finely chopped toasted hazelnuts, skins removed",
" ",
"For Ganache:",
"1/2 cup heavy cream",
"6 ounces semisweet chocolate, chopped",
" ",
"For Frosting:",
"1 (12 ounce) package white chocolate chips",
"1 cup unsalted butter, at room temperature",
"2 (8 ounce) packages cream cheese, softened",
"2 teaspoons vanilla extract",
"food coloring, if desired (optional)"
],
"source_url": "http://allrecipes.com/Recipe/Zuccotto-Cupcakes/Detail.aspx",
"recipe_id": "521",
"image_url": "http://static.food2fork.com/8942993716.jpg",
"social_rank": 80.98230224538719,
"publisher_url": "http://allrecipes.com",
"title": ""Zuccotto" Cupcakes"
}
};
db.recipes.save(r2); // save that thing
db.recipes.find({"recipe.ingredients":/eg/i}); // just need to say recipe. now (need the quotes arpund the key bc of the dot)
@cfmays
Copy link

cfmays commented Feb 16, 2015

Good grief. I tried recipes.ingredients, but I did not see that I needed the quotes and the error msg didn't help me. Thanks for your help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment