Mongodb Object id displaying weiredly - mongodb

I have Database in which there are few collections.
In one collection the _id field is displaying as
"_id" : ObjectId("58b0196947c4f14490d18741")
While other collection is Displaying something like this
"_id" : {
"timestamp" : 1487170280,
"machineIdentifier" : 8083947,
"processIdentifier" : 12168,
"counter" : 5289607,
"time" : NumberLong("1487170280000"),
"date" : NumberLong("1487170280000"),
"timeSecond" : 1487070280
}
Not sure what is root cause for it.

Related

Get the field having same value in a mongodb collection

I have a document in my mongo db collection :
{
"_id" : ObjectId("xxxxxxx"),
"ID" : "a_11",
"details_list" : [
{
"detail" : "detail_1",
"link" : "https://xxxxx/yyy"
},
{
"detail" : "detail_2",
"link" : "https://xxxxx/zzz"
}
],
"name" : "xyz"
}
I want to get the "name" of all other docs where details_list (which is list of dictionary itself) matches with any other document in the same collection.
I tried using $where clause as well. db.collection.find({"$where": details_list == details_list}) but it did not returned any value.

And Operator in Criteria not working as expected for nested documents inside aggregation Spring Data Mongo

I am trying to fetch total replies where read values for a replies is true. But I am getting count value as 3 but expected value is 2 (since only two read value is true) through Aggregation function available in Spring Data Mongo. Below is the code which I wrote:
Aggregation sumOfRepliesAgg = newAggregation(match(new Criteria().andOperator(Criteria.where("replies.repliedUserId").is(userProfileId),Criteria.where("replies.read").is(true))),
unwind("replies"), group("replies").count().as("repliesCount"),project("repliesCount"));
AggregationResults<Comments> totalRepliesCount = mongoOps.aggregate(sumOfRepliesAgg, "COMMENTS",Comments.class);
return totalRepliesCount.getMappedResults().size();
Using AND Operator inside Criteria Query and passed two criteria condition but not working as expected. Below is the sample data set:
{
"_id" : ObjectId("5c4ca7c94807e220ac5f7ec2"),
"_class" : "com.forum.api.domain.Comments",
"comment_data" : "logged by karthe99",
"totalReplies" : 2,
"replies" : [
{
"_id" : "b33a429f-b201-449b-962b-d589b7979cf0",
"content" : "dasdsa",
"createdDate" : ISODate("2019-01-26T18:33:10.674Z"),
"repliedToUser" : "#karthe99",
"repliedUserId" : "5bbc305950a1051dac1b1c96",
"read" : false
},
{
"_id" : "b886f8da-2643-4eca-9d8a-53f90777f492",
"content" : "dasda",
"createdDate" : ISODate("2019-01-26T18:33:15.461Z"),
"repliedToUser" : "#karthe50",
"repliedUserId" : "5c4bd8914807e208b8a4212b",
"read" : true
},
{
"_id" : "b56hy4rt-2343-8tgr-988a-c4f90598h492",
"content" : "dasda",
"createdDate" : ISODate("2019-01-26T18:33:15.461Z"),
"repliedToUser" : "#karthe50",
"repliedUserId" : "5c4bd8914807e208b8a4212b",
"read" : true
}
],
"last_modified_by" : "karthe99",
"last_modified_date" : ISODate("2019-01-26T18:32:41.394Z")
}
What is the mistake in the query that I wrote?

How to view an object embedded with a document in mongodb

> db.orders.find({})
{ "_id" : ObjectId("5b78b933d62e262ddb055509"), "user_id" : "5b16d96a74be42566844e0b4", "game_id" : "5b11c56c6c71dc44976fba55", "seats" : { "_id" : ObjectId("5b78b933d62e262ddb05550a") }, "__v" : 0 }
{ "_id" : ObjectId("5bb135638625d21c0883fe1d"), "user_id" : "5b16d96a74be42566844e0b4", "game_id" : "5b11c56c6c71dc44976fba61", "seats" : { "_id" : ObjectId("5bb135638625d21c0883fe1e") }, "__v" : 0 }
The above is the output of find command on the orders stored in my Mongo Instance.
seats is an array of objects embedded in the orders schema. How can I view and fetch the array stored in Object seats?
seats is a cross-reference field which contains the ObjectIds to anther collection.
Are you using Mongoose? If so you need to use the populate method to fill in the objects on find etc.

MongoDB Check if a Collection contains a Key

I am using the MongoDB Async Driver to create a little Program were i have to save a User-Profile in the Database.
So i have a Database looking like this
`{
"_id" : ObjectId("5a1743f086060f1bbc319cdb"),
"id" : "aUser1",
"SomeArray" : {
"Name" : "SomeName"
}
}
{
"_id" : ObjectId("5a1743f186060f1bbc319cdc"),
"id" : "aUser2",
"SomeArray" : {
"Name" : "SomeName"
}
}
{
"_id" : ObjectId("5a1743f286060f1bbc319cdd"),
"id" : "aUser3",
"SomeArray" : {
"Name" : "SomeName"
}
}`
And now i try to figure out if the User 1 already has created a Profile.
Whith the old driver i would know how but with the , for me new, Async-Driver it sems a little bit tricky, do i have to make an Callback for this ?

mongodb get elements which was inserted after some document

I have a document and I need to query mongodb database to return me all the documents which was inserted after current document.
Is it possible and how to do that query?
If you do not override the default _id field you can use that objectID (see the mongodb docs) to make a comparison by time. For instance, the following query will find all the documents that are inserted after curDoc has been inserted (assuming none overwrite the _id field):
>db.test.find({ _id : {$gt : curDoc._id}})
Note that these timestamps are not super granular, if you would like a finer grained view of the time that documents are inserted I encourage you to add your own timestamp field to the documents you are inserting and use that field to make such queries.
If you are using Insert time stamp as on of the parameter, you can query like below
> db.foo.find()
{ "_id" : ObjectId("514bf8bbbe11e483111af213"), "Name" : "abc", "Insert_time" : ISODate("2013-03-22T06:22:51.422Z") }
{ "_id" : ObjectId("514bf8c5be11e483111af214"), "Name" : "xyz", "Insert_time" : ISODate("2013-03-22T06:23:01.310Z") }
{ "_id" : ObjectId("514bf8cebe11e483111af215"), "Name" : "pqr", "Insert_time" : ISODate("2013-03-22T06:23:10.006Z") }
{ "_id" : ObjectId("514bf8eabe11e483111af216"), "Name" : "ijk", "Insert_time" : ISODate("2013-03-22T06:23:38.410Z") }
>
Here my Insert_time corresponds to the document inserted time, and following query will give you the documents after a particular Insert_time,
> db.foo.find({Insert_time:{$gt:ISODate("2013-03-22T06:22:51.422Z")}})
{ "_id" : ObjectId("514bf8c5be11e483111af214"), "Name" : "xyz", "Insert_time" : ISODate("2013-03-22T06:23:01.310Z") }
{ "_id" : ObjectId("514bf8cebe11e483111af215"), "Name" : "pqr", "Insert_time" : ISODate("2013-03-22T06:23:10.006Z") }
{ "_id" : ObjectId("514bf8eabe11e483111af216"), "Name" : "ijk", "Insert_time" : ISODate("2013-03-22T06:23:38.410Z") }
>