I can obtain a non embedded field from my document through jade by doing:
each user, i in gauss_buff
tr
td #{user.build_num} // where build_num is an element in my document.
However, how should one access an embedded document via Jade. Doing
user.embedded_doc_name.field
didn't work.
I used #{user.not_embedded_field} and I receive:
{
"not_embedded_field" : not_embedded_field_value,
"embedded_document": [{
"embedded_field1" : some_value
"embedded_field2" : some_other_value
I have hunted around, but can't find how to do this anywhere. Any tips would be appreciated.
Then use it like an array: #{user.embedded_document[0].embedded_field1}
Related
How can I find object ID of MMT4 by nodeJS and mongoDB ?
Thank you for your help ❤️
Hi I worked on your question and if you are using mongodb and nodejs you can do the following stpes:
using $match and $unwind aggregations to find the object that match with title MMT4:
db.collection.aggregate([{$unwind: "$children"}, {$match: {"children.title": "MMT4"}}])
Notice: I wrote the above syntax in mongodb shell you can write it in JS with mongoose library.
the result is an object with children array that contain just MMT4 object. you can iterate in this single element array using JS to get _id of the MMT4
reference: click here
{
"_id" : ObjectId("5f0083848f162b38900dc113"),
"isEmailVerified" : false,
"isProfileSetup" : true,
"my_events" : [
ObjectId("5f005a63b5524eb74813de11"),
ObjectId("5f005a5bb5524eb74813de0c"),
ObjectId("5f017dfcf8e6d8615cddfd6f")
]
}
I have this document in user's collection and i am trying to paginate the array my_events only. I am sorry if this is a stupid question.
Firstly is this possible to paginate this array without event fetching it completely from the db and if yes please share the way here.
{{url_local}}/api/event?user_id=5f0083848f162b38900dc113&page=1&limit=2
Above call should find the user with mentioned user_id and should return only these values :-
ObjectId("5f005a63b5524eb74813de11"),
ObjectId("5f005a5bb5524eb74813de0c")
And,
{{url_local}}/api/event?user_id=5f0083848f162b38900dc113&page=2&limit=2
it should return this :-
ObjectId("5f017dfcf8e6d8615cddfd6f")
This can be achieved with aggregation in MongoDB, pls check https://docs.mongodb.com/manual/reference/operator/aggregation/arrayElemAt/
But in general, the advantage of paging will be lost as aggregations are heavy on the database, you should consider to change the document structure and create a collection for subdocuments and add a reference to your parent document if you can.
TLDR
Is there a way to limit queryByExample to a collection in NodeJS?
Problem faced
I have a complex query with some optional fields (i.e. sometimes some search fields will be omitted). So I need to create a query dynamically, e.g. in JSON. QueryByExample seems to be the right tool to use here as it gives me that flexibility to pass a JSON. However my problem is that I would like to limit my search to only one collection or directory.
e.g. I was hoping for something like
searchJSON = {
title: { $word: "test" },
description: { $word: "desc" }
};
//query
db.documents.query(qb.where(
qb.collection("collectionName"),
qb.byExample(searchJSON)
)).result()...
In this case searchJSON could have been built dynamically, for example maybe sometimes title may be omitted from the search.
This doesn't work because the query builder only allows queryByExample to be the only query. But I'd instead like to built a dynamic search query which is limited to a collection or directory.
At present, I think you would have to express the query with QueryBuilder instead of Query By Example using
qb.and([
qb.collection('collectionName'),
qb.word('title', 'test'),
qb.word('description', 'desc')
])
See http://docs.marklogic.com/jsdoc/queryBuilder.html#word
That said, it should be possible for the Node.js API to relax that restriction based on the fixes in MarkLogic 9.0-2
Please file an issue on https://github.com/marklogic/node-client-api
I have a sample schema like this -
Comment.add({
text:String,
url:{type:String,unique:true},
username:String,
timestamp:{type:Date,default:Date}
});
Feed.add({
url:{type:String, unique:true },
username:String,
message:{type:String,required:'{PATH} is required!'},
comments:[Comment],
timestamp:{type:Date,default:Date}
});
Now, I don't want to expose the _id fields to the outside world that's why I am not sending it to the clients anywhere.
Now, I have two important properties in my comment schema (username,url)
What I want to do is update the content of the sub document that satisfies
feed.url
comment.url
comment.username
if the comment.username is same as my client value req.user.username then update the comment.text property of that record whose url was supplied by client in req.body.url variable.
One long and time consuming approach I thought is to first find the feed with the given url and then iterating over all the subdocuments to find the document which satisfies the comment.url==req.body.url and then check if the comment.username==req.user.username if so, update the comment object.
But, I think there must be an easier way of doing this?
I already tried -
db.feeds.update({"username":"harshitladdha93#gmail.com","comments.username":"harshitladdha3#gmail.com","comments.url":"test"},{$set:{"comments.$.text":"updated text 2"}})
found from http://www.tagwith.com/question_305575_how-to-find-and-update-subdocument-within-array-based-on-parent-property
but this updates even when the comments.url or comments.usernamematches other sub documents
and I also tried
db.feeds.distinct("comments._id",{"comments.url":req.body.url})
to find the _id of document associated with the url but it returns all the _id in the subdocument
First off - you should not rely on _id not being seen by the outside world in terms of security. This is a very bad idea for a multitude of reasons (primarily REST and also the fact that it's returned by default with all your queries).
Now, to address your question, what you want is the $elemMatch operator. This says that you're looking for something where the specified sub-document within an array matches multiple queries.
E.g.
db.feeds.update({
"username":"harshitladdha93#gmail.com",
comments: {
$elemMatch: {
username: "harshitladdha3#gmail.com",
url: "test"
}
}
}, {$set: {"comments.$.text":"updated text 2"}})
If you don't use $elemMatch you're saying that you're ok with the document if any of the comments match your query - i.e. if there is a comment by user "harshitladdha3#gmail.com", and separate comment has a url "test", the document will match unless you use $elemMatch
I have a document:
{
"_id":ObjectId("someID")
"email":"john#example.com"
}
I would like to retrieve "john#example.com" so that I can pass this value to the browser.
How do I do that?
I saw these SO posts, but I don't understand the solution at all:
How to get string value inside a Mongodb document?
How to get value from a MongoDB document