Retrive only part of a subdocument in mongodb - mongodb

I have some data that looks like this:
{
"_id" : "5227aa5d9881d31cd3aa0e78",
"Message" : "This is a message 5:47 PM",
"IssuedAt" : ISODate("2013-09-04T21:47:09.932Z"),
"Users" : [
{
"_id" : "dhBHuZL9M+hqtKIx14iu",
"IsRead" : true
},
{
"_id" : "SOMSOMOMODJFJDFKJKDJF",
"IsRead" : false
}
]
}
and I was hoping retrieve the following about one user:
{
"_id" : "5227aa5d9881d31cd3aa0e78",
"Message" : "This is a message 5:47 PM",
"IssuedAt" : ISODate("2013-09-04T21:47:09.932Z"),
"IsRead" : false
}
I tried this but it will only return the record with the whole subdocument:
db.collection.find({"Users": {$elemMatch: {"_id": 'dhBHuZL9M+hqtKIx14iu'}}}, {"Message": 1, "Users.$.IsRead": 1}).pretty()
Is there a way to get what I am looking for without using aggregate?

In the current requirement , i think you have to redesign your schema . Schema has to be more efficient for your query. May be Schema for the current requirement will be to have User Collection
{
"_id": "dhBHuZL9M+hqtKIx14iu" ,
"Name" : "Test",
"ReadMessages" [{}],
"UnReadMessage" [{}]
}
Now for any users you can get the read messages and unread message very easy . One update statement you have to fire to move the message from UnRead to read. But it will happen once and rest read will be very fast. I know it will not solve your problem but may help you wish to change the schema.

Related

Query on all descendants of node

Given a collection of MongoDb documents with a property "myContacts" like this:
{
"_id": 123,
"myContacts" : {
"contacts" : {
"10" : {
"_id" : NumberLong(10),
"name" : "c1",
"prop" : true
},
"20" : {
"_id" : NumberLong(20),
"name" : "c2"
},
}
}
}
I want to select all documents, where at least one contact lacks the "prop" field.
I figured out a general query:
db.getCollection('xyz').find({ 'myContacts.contacts.???.prop': { $exists: false } })
The problem is that IDs of the contacts are part of the path and I cannot know them ahead. I want sth like 'myContacts.contacts.$anyChild.prop', but cannot find sth similar in the mongo docs.
Does it mean there is no way to do it?
PS: I cannot change the document structure, a live app uses it. I've spent some time with Google and my bet it's not possible. I however would like an opinion from people who have experience with Mongo.
Thank you guys for helpful comments, this got me going! I could get the results I wanted with:
db.getCollection('xyz').aggregate([{$project: {_id:1, contacts:{$objectToArray: "$myContacts.contacts"}}}, {$match: {"contacts.v.prop" : null}}])

using upsert in array search in mongodb

I am working on mongodb and making a database structure as:
{
"_id" : ObjectId("5e9fef05c0228a50befba0d7"),
"name" : "sanghm",
"id" : "3456",
"dep" : {
"dep1" : "ops",
"dep2" : "analytics"
},
"data" : [
{
"date" : "25-apr-2020",
"log" : [
{
"machine" : "windows-user1",
"task" : "excel",
"time": "10:00am"
}
{
"machine" : "windows-user1",
"task" : "email",
"time": "11:00am"
}
]
}
]
}
to push every new task on database, i'm using a query like
date = '25-apr-2020'
db.inventory.update({id:'3456'},{{$push:{'data.$[t].log':{machine:'windows-user1',task:'new task',time:'3:00pm'}}},{arrayFilters:[{"t.date":date}]}})
this query pushing new task to date array.
if in case the date changes to '26-apr-2020' then can we use same query to first add new date filed and then push data for the same.
i have read about {upsert: true} but dont know how to use it in my case
The double nesting is weird. Usually all of the events would be stored flat in the top level data array and one would produce the grouping by date (if desired) at query time.

mongodb select using association

I have two collections, user_logs and users, user_logs documents have user_id field so I need some data from user_logs but in the same query I want to check if some other field from user related to the current user_log is empty. How should I do this?
A query can only access one collection at a time. Mongodb doesn't support joins.
They that's why they recommend that you embed the referenced data inside the document.
If the logs documents for each user isn't too big, then you can change the embed that info inside the user collection.
Giving you something like this.
Embedded User Collection:
{
user_id : "uid1",
logs : [
{ message : "Error: System shutdown", date : "2014-11-11" },
{ message : "Error: System shutdown", date : "2014-11-13" }
]
}
However, if you want to keep your current structure then you're going to have to perform two queries to find related info between the users and user_logs collections.
Example
db.user_logs.insert([
{ _id : "ul1", log : "code 1", user_id : "u1" },
{ _id : "ul2", log : "code 2", user_id : "u1" }
]);
db.users.insert([
{ _id : "u1", name : "bob", user_logs_id : "ul1" },
{ _id : "u2", name : "smith", user_logs_id : "ul2" }
]);
var userId = db.user_logs.findOne({}).user_id;
db.users.findOne({ _id : userId })
//outputs
{ "_id" : "u1", "name" : "bob", "user_logs_id" : "ul1" }

Filtering Mongo items by multiple fields and subfields

I have the following items in my collection:
> db.test.find().pretty()
{ "_id" : ObjectId("532c471a90bc7707609a3d4f"), "name" : "Alice" }
{
"_id" : ObjectId("532c472490bc7707609a3d50"),
"name" : "Bob",
"partner_type1" : {
"status" : "rejected"
}
}
{
"_id" : ObjectId("532c473e90bc7707609a3d51"),
"name" : "Carol",
"partner_type2" : {
"status" : "accepted"
}
}
{
"_id" : ObjectId("532c475790bc7707609a3d52"),
"name" : "Dave",
"partner_type1" : {
"status" : "pending"
}
}
There are two partner types: partner_type1 and partner_type2. A user cannot be accepted partner in the both of types. But he can be a rejected partner in partner_type1 but accepted in the another, for example.
How can I build Mongo query that fetches the users that can become partners?
When your user can only be accepted in one partner-type, you should turn it around: Have a field accepted_as:"partner_type1" or accepted_as:"partner_type2". For people who aren't accepted yet, either have no such field or set it to null.
In both cases, your query to get any non-accepted will then be:
{
data.accepted_as: null
}
(null matches both non-existing fields as well as fields explicitly set to null)
For me the logical schema would be this:
"partner : {
"type": 1,
"status" : "rejected"
}
At least that keeps the paths consistent between documents.
So if you want to stay away from using mapReduce type methods to find out "which field" it is on, and otherwise use plain queries and the aggregation pipeline, then don't vary field paths on documents. If you alter the "data" then that is the most consistent form.

Array query and return

My first adventure into Mongo. Please save me some time by answering the following. This is the schema.
"_id" : 1,
"FullName" : "Full Name",
"Email" : "email#email.com",
"FacebookId" : NumberLong(0),
"LastModified" : ISODate("2012-04-11T09:26:10.955Z"),
"Connections" : [{
"_id" : 7,
"FullName" : "Fuller name",
"Email" : "connections#email.com",
"FacebookId" : NumberLong(0),
"LastModified" : ISODate("0001-01-01T00:00:00Z")
},
....
Given an id of a single top user, i'd like to return all of the Emails in the Connections array, and preferably, just the emails. What's the querystring? Much obliged!
You can't get only values from the sub-objects in MongoDB.
If you do a query like this:
db.test.find({"_id": 1}, {"Connections.Email":1});
you will get this kind of response:
{
"_id": 1,
"Connections" : [ {"Email":"connections#email.com"},
{"Email":"foo#example.com"} ]
}
This is the closest you can get with a simple query and field selection from MongoDB.
You can then filter out the e-mails values in your code with a simple foreach.