I want to get particular object in array. My database structure is shown in below image
In this i am trying to get particular object like this
db.users.find({article:{$elemMatch:{articleid:"WMCA268937"}}})
but nothing is getting, someone please help.
Related
Hello guys i'm trying to query a collection made in firebase cloudfirestore.
How can i create custom queries for objects inside an array.
This is how looks my collection:
"someRestaurantId": [{id:1,desc:"test",creationDate:"12/12/2020"},{id:2,desc:"test",creationDate:"12/12/2020"},{id:3,desc:"test",creationDate:"12/12/2020"}]
I have two problems how i can get only object for id:1 and then querie in a date range using creationDate input?
So far I been getting the full object based in the key: someRestaurantId, and then iterating in my nodejs function, but it could be many of restaurantsIds and many object and i don't want to kill the performance of the query if the object has thousands of results.
Unfortunately, there is now way to retrieve less than one document.
I am using node.js as a scripting language and I am also using nunjucks as the template engine. I have a weird situation going where some of the data that I retrieve from the MongoDB database is not being printed into document.
As you see hear these are the values that I want to print to the web page
But when you look at the web page, only some of the data has been printed out and the other information is missing.
I console.logged the data to prove that the values are in the database
A weird thing is that if you write the whole object into the code, like so
It will output all the data in one block. That includes the month, the year, and slug property that I am trying to output to the page. Yet, it only does that if I print out the whole object
I found out what was the problem. In my mongoose schema, I did not have the properties listed like slug, month, or date. So when I tried to retrieve data from those properties it did not output them. So If you are having similar problems make sure the properties are declared in your mongoose schema
While updating a document with embedded arrays, I am getting DivergentArrayError. I am updating array elements of the document.
Can someone please help me by explaining what does this error mean. I am not able to find any relevant information anywhere :(.
I am working on an application that uses MongoDB for data persistence, alongside the mongoose library to allow angularjs to communicate with it. I'm using Postman to test my routes, and that's where the problem comes in.
In MongoDB, I inserted an entry thusly:
db.posts.insert{{id:6, author:"Blah", area:"Sport", body:"bleh", user_id:6, comments:[{author:"foo", body:"bar"}]})
I then queried it through Postman, and it successfully returned the Post, but there was a problem. The comments array had been returned as a strange data type, object Object, and I could not access it from my HTML files.
This is what Postman retrieved:
{"_id":"5536112cffc7bf00b2585d24","id":6,"author":"Blah","area":"Sport","body":"bleh","user_id":6,"comments":["[object Object]"]}]
I want to be able to access the author and body elements of comments independently, or else to be able to get at them from my http files. Does anyone know how I might stop this Object-ification from happening, or failing that, how I might query the data from this new Object?
Thanks.
Instead of passing complete db object we can pass required parameters. below is the example to form the required parameters.
res.json({"author":item.author,"body":item.body});
Here You can find call back function and array conversion from the cursor
Look at the example: http://pastie.org/5901177
In that code I create 3 clients and push them into user.clients array. After that I added one more client on a top of user.clients array. So when I do
Client.where('_id').in(user.clients)
I'm getting clients in wrong order. Look at this please via console.
Does anybody know what's problem in my code?
The order of the returned documents is not affected by the order of the array elements of the $in operator used to identify them in a find.
You always need to explicitly sort if you want a specific document order from a find.