Saturday, 15 September 2012

node.js - How to find a sub document in mongoose without using _id fields but using multiple properties -


I have a sample schema like this -

  comment.Add ({text: String, url: {type: string, unique: true}, user name: string, timestamp: {type: date, default: date}}); Feed.add ({url: {type: string, unique: true}, user name: string, message: {type: string, required: '{PATH} is required!'}, Comment: [comment], timestamp: { Type: date, default: date}});  

Now, I do not want to disclose the _id field to the outside world, this is why I am not sending it to customers anywhere. Now, there are two important properties in my comment schema (username, url), what I have to do, update the contents of the subdocument which

  1. feed.url
  2. Comment.url
  3. Comment. Username

If comment.username is similar to my customer value req.user I username then comment. Update the asset of that record, whose URL was passed by the client in the req.body.url variable.

I thought it takes a long time, searching the feed with the given URL, and then the document on all the subdocuments that comment.url == req.body.url And comment.username == req.user.username if so, update the comment object. But, I think that should be an easy way to do this? I already tried -

  db.feeds.update ({"username": "harshitladdha93@gmail.com", "comments.username": "harshitladdha3@gmail.com", "Comments Url": "test"}, {$ set: {"comments. $ Text": "Update text2"}})  

found

But this update even if comments.url or comments.username matches other subdocuments

and I also tried

  Db.feeds.distinct ("comments._id", {code._id "," code.url ": req.body.url}}  

to < Url to find the document associated with the code> but all this _id sub-document

First of all - you should not be seen in the outside world in terms of security _id This is a very bad idea for many reasons (mainly comfort and also that comes back by default with all your questions).

Now To solve your question, what do you want $ Elem match operator says that you have the bottom of any such thing Words are, where specified sub-document within an array that matches the number of queries.

  db.feeds.update ({"username": "harshitladdha93@gmail.com", comments: {$ elemMatch: {username: "harshitladdha3 @ Gmail.com ", url:" test "}}}}, {$ Set: {" comments. $ Text ":" Update Text 2 "}})  

> $ ElemMatch You are saying that the document is okay if a comment matches your query - that means if the user has a comment by "harshitladdha3@gmail.com", and the url "test" in different comments , So long as you do not use $ elemMatch


No comments:

Post a Comment