How to lookup in another collection with objectID - database

collection 'bookborrow'
"_id" : ObjectId("62ebbd1f4d6ab77fe3dad494"),
"userId" : "62ea90c6691228b6157cc0f9",
"data" : {
"todayDate" : "04/08/2022",
"dateofreturn" : "19/08/2022",
"bookid" : "62e53c1ff5d4c45fb7853d9e"
}
}
collection 'bookdetails'
{
"_id" : ObjectId("62e53c1ff5d4c45fb7853d9e"),
"bookname" : "How To Own Your Own Mind",
"bookauthor" : "Napoleon Hill",
"counterbooks" : "2"
}
output look like
"bookname" : "How To Own Your Own Mind",
"todayDate" : "04/08/2022",
"dateofreturn" : "19/08/2022",
bookid is uniq id .it's bookdetails match _id

Related

Find element inside array and return only array

I'm new to MongoDB, so i'm having some newbie problems.
I have this document on DB
{
"_id" : ObjectId("5a7ba73dbf27878474ac7682"),
"Enterprise" : "SpaceX",
"Address" : "qwerty",
"users" : [
{
"name" : "Elon",
"number" : "123456",
"email" : "elon#mars.com",
"user" : "elon",
"pass" : "byeroadster"
},
{
"name" : "Larry",
"number" : "3215465",
"email" : "larry#google.com",
"user" : "larry",
"pass" : "googlepassword"
}
]
}
And i'm doing this query
var db = client.db('hauz_manager');
var findUser = db.collection('system_enterprise');
findUser.find({'users.user': data.user},{contatos: 1}).toArray(function(err, result){
console.log(result.users);
});
With that, i'm having like
Enterprise: "SpaceX"
...
users: [{Object}]
And, i'm not able to do some result.users, returns undefined.
tnx.
console.log(result.users)
I think your callback return array.
You should check callback.
Do you need a loop?

Mongo Find one set of field in nested object

i have a follwing structure in mongo db
{
"_id" : ObjectId("58e8d3323fe482ef368b4567"),
"productId" : "poe10001134",
"batchdetails" : [
{
"batchidId" : "BD0166",
"batchno" : "BbfN-1444-SBRPX"
},
{
"batchidId" : "BD0167",
"batchno" : "Bff-8444-kfkff"
},
{
"batchidId" : "BD01447",
"batchno" : "Bff-8415-kfkff"
}
]
}
i want to achieve following result
"productId" : "poe10001134"
"batchno" : "Bff-8415-kfkff"
when i execute following query
db.getCollection('table').findOne( {"batchdetails.batchidId" : "BD01447"},{productId:1,"batchidId.batchno":1})
it returns all the batch no
"productId" : "poe10001134"
"batchno" : "BbfN-1444-SBRPX"
"batchno" : "Bff-8444-kfkff"
"batchno" : "Bff-8415-kfkff"
Do it like this:
db.getCollection('table').findOne( {"batchdetails.batchidId" : "BD01447"},
{productId:1,"batchidId.batchno.$":1})
The $ in the projection contains the index of the batchno array element that was matched in the query.

Update a single field to array in mongodb using JAVA

I have documents of below format:
{
"_id" : ObjectId("559bae5df3fcf46e468a9779"),
"CPF" : "129-87-716",
"NAME" : "Donald Carver",
"CONTACT" : 1827626847,
"ADDRESS" : "937 Hamburg Path",
"EMAIL_ADDRESS" : "constpherd#yah00.net",
"CARDS" : {
"CREDIT_CARD_NUMBER" : "1678-768-420-4188",
"CARD_TYPE" : "IVISA",
"EXPIRY_DATE" : "Mon May 21 15:23:53 IST 2018"
}
}
I want to update an array of document .
"TRANSACTIONS":[{"TRANSACTION_NUMBER":"1","TRANSACTION_DATE":"07/07/2015","BANK_ID":"200","AMOUNT":100},
{"TRANSACTION_NUMBER":"2","TRANSACTION_DATE":"08/07/2015","BANK_ID":"0","AMOUNT":200},
{"TRANSACTION_NUMBER":"3","TRANSACTION_DATE":"06/07/2015","BANK_ID":"200","AMOUNT":100}
]
}
And it should look like below:
{
"_id" : ObjectId("559bae5df3fcf46e468a9779"),
"CPF" : "129-87-716",
"NAME" : "Donald Carver",
"CONTACT" : 1827626847,
"ADDRESS" : "937 Hamburg Path",
"EMAIL_ADDRESS" : "constpherd#yah00.net",
"CARDS" : {
"CREDIT_CARD_NUMBER" : "1678-768-420-4188",
"CARD_TYPE" : "IVISA",
"EXPIRY_DATE" : "Mon May 21 15:23:53 IST 2018"
}
"TRANSACTIONS":[{"TRANSACTION_NUMBER":"1","TRANSACTION_DATE":"07/07/2015","BANK_ID":"200","AMOUNT":100},
{"TRANSACTION_NUMBER":"2","TRANSACTION_DATE":"08/07/2015","BANK_ID":"0","AMOUNT":200},
{"TRANSACTION_NUMBER":"3","TRANSACTION_DATE":"06/07/2015","BANK_ID":"200","AMOUNT":100}
]
}
}
This is what I am trying to do it using java:
String[] creditcard = line.split(cvsSplitBy);
DBObject listItem = new BasicDBObject("TRANSACTION", new BasicDBObject("TRANSACTION_NUMBER",creditcard[1])
.append("TRANSACTION_DATE",creditcard[2])
.append("TRANSACTION_TYPE",creditcard[3])
.append("BANK_ID",creditcard[4])
.append("AMOUNT",creditcard[5])
);
DBObject updateQuery = new BasicDBObject("$push", listItem);
userimage.update(new BasicDBObject().append("CARDS.CREDIT_CARD_NUMBER", creditcard[0]), updateQuery);
The program updates one row when I run for the first time.When I run second time the program does not insert any doc into the array ...
I am not able to find out the logical error in the program..

how to do custom filter in angular JS and what should be the return type

I am using MEAN stack and trying to filter the data from the ng-repeat.
Here goes my HTML code
<section data-ng-controller="BookingsController" data-ng-init="find()">
<div data-ng-repeat="x in bookings|filter:select_booking" data-ng-href="#!/bookings/{{booking._id}}" class="list-group-item">
<label>Booking ID :</label>{{x.booking_id}}
<label>Membership ID :</label>{{x.membership_id}}
<label>Check Out Date :</label>{{ x.checkout_time | date : "longDate" }}
<label>Rooms :</label><span ng-repeat="room in booking.room_id">{{room}} </span>
<button class="pull-right" value="Block" ng-click="checkout(x)">Check Out</button>
<button class="pull-right" style="margin-right:10px" value="Block" ng-click="services(x)">Add Services</button>
<br>
</div>
</section>
My filter name in the above code is select_booking.
The code for ng-controller goes here
$scope.checkindetails = CheckinDetails.query();/*accessing the mongo db values*/
$scope.select_booking = function (a) {
var f = 0;
jQuery.each($scope.checkindetails, function (key, val) {
if (val.booking_id == a.booking_id) {
console.log("in if");
console.log(a.booking_id);
return true;
}
});
};
My Console.log result:
bookings.client.controller.js:300in if
bookings.client.controller.js:301 ASHISH_BOOK_002
bookings.client.controller.js:300 in if
bookings.client.controller.js:301 ASHISH_BOOK_000
bookings db:here referred as a
{
"_id" : ObjectId("54f02f7ba3c39d8c106bd3f7"),
"user" : ObjectId("54eede3363b3e60c2133fe06"),
"created" : ISODate("2015-02-27T08:48:59.890Z"),
"date_of_cancellation" : "",
"cancelled" : "No",
"points" : 0,
"booking_id" : "ASHISH_BOOK_000",
"checked_out" : "Yes",
"booking_date" : ISODate("2015-02-27T08:48:59.890Z"
"adult_count" : 4,
"room_id" : [
"101",
"102"
],
"rooms_count" : 2,
"resort_id" : "ASH_RESORT001",
"checkout_time" : "2015-02-28T18:30:00.000Z",
"checkin_time" : "2015-02-26T18:30:00.000Z",
"guest" : "no",
"membership_id" : "4",
"__v" : 0
}
{
"_id" : ObjectId("54f560e2e0d3bed8069214f5"),
"user" : ObjectId("54ec4eb13d50decc1f5307b4"),
"created" : ISODate("2015-03-03T07:21:06.386Z"),
"date_of_cancellation" : "",
"cancelled" : "No",
"points" : 0,
"booking_id" : "ASHISH_BOOK_001",
"checked_out" : "Yes",
"booking_date" : ISODate("2015-03-03T07:21:06.386Z"
"adult_count" : 4,
"room_id" : [
"100",
"101"
],
"rooms_count" : 2,
"resort_id" : "ASH_RESORT002",
"checkout_time" : "2015-03-03T18:30:00.000Z",
"checkin_time" : "2015-03-02T18:30:00.000Z",
"guest" : "no",
"membership_id" : "1",
"__v" : 0
}
{
"_id" : ObjectId("54f6942195002c140700dac0"),
"user" : ObjectId("54ec4eb13d50decc1f5307b4"),
"created" : ISODate("2015-03-04T05:12:01.211Z"),
"date_of_cancellation" : "",
"cancelled" : "No",
"points" : 0,
"booking_id" : "ASHISH_BOOK_002",
"checked_out" : "Yes",
"booking_date" : ISODate("2015-03-04T05:12:01.211Z"
"adult_count" : 2,
"room_id" : [
"103"
],
"rooms_count" : 1,
"resort_id" : "ASH_RESORT001",
"checkout_time" : "2015-03-04T18:30:00.000Z",
"checkin_time" : "2015-03-03T18:30:00.000Z",
"guest" : "no",
"membership_id" : "1",
"__v" : 0
}
checkindetails db:
{
"_id" : ObjectId("54f06257d75533c01add0cf0"),
"user" : ObjectId("54ec4eb13d50decc1f5307b4"),
"created" : ISODate("2015-02-27T12:25:59.185Z"),
"id_proof_type" : "abcd",
"address" : "bangalore",
"guest_name" : "Harshavardhan",
"booking_id" : "ASHISH_BOOK_000",
"__v" : 0
}
{
"_id" : ObjectId("54f696b095002c140700dac4"),
"user" : ObjectId("54ec4eb13d50decc1f5307b4"),
"created" : ISODate("2015-03-04T05:22:56.498Z"),
"id_proof_type" : "okoko",
"address" : "okoko",
"guest_name" : "kok",
"booking_id" : "ASHISH_BOOK_002",
"__v" : 0
}
I am able to reach till console.log(a.booking_id);
Here I am comparing the booking_id of a(input parameter) with the booking_id of checkindetails and I want the result value of matching booking_id.
The console.log is providing me the correct answer but when I return that console.log value I am getting a blank answer.
Please Help me to find the correct One.
Thanks in advance.
According to Angularjs documentation a filter:
Selects a subset of items from array and returns it as a new array.
Think of the filter as a subset of your original data, that subset is defined within the function that is called when you use that filter.
So in your example you would define your filter "select_booking" to return the filtered values. They can be partial matches by default, or exact matches by adding.."...:true" to the end.
If you want to match the id, you should return the actual id, not "true".
return a.booking_id;
If your filter returns a single id, your ng-repeat will show all bookings with that id. As a side note, if it is an "id" I would use the exact match like...
"book in bookings|filter:{id:select_booking}:true"
That would be how to set it up. Your return type is what you are filtering. If I'm filtering an array of numbers I return numbers. If I'm filtering text, I return text ect.

update nth document in a nested array document in mongodb

I need to update a document in an array inside another document in Mongo DB
{
"_id" : ObjectId("51cff693d342704b5047e6d8"),
"author" : "test",
"body" : "sdfkj dsfhk asdfjad ",
"comments" : [
{
"author" : "test",
"body" : "sdfkjdj\r\nasdjgkfdfj",
"email" : "test#tes.com"
},
{
"author" : "hola",
"body" : "sdfl\r\nhola \r\nwork here"
}
],
"date" : ISODate("2013-06-30T09:12:51.629Z"),
"permalink" : "jaiho",
"tags" : [
"jaiho"
],
"title" : "JAiHo"
}
Q1) Update email of 0th element of comments array
db.posts.update({"permalink" : "haha"},{$set:{"comments.0.email":1}})
This doesn't throw any exception but doesn't update anything as well
Q2) Add a field on nth element of comments array number_likes
db.posts.update({"permalink" : "haha"},{$inc:{"comments.0.num_likes":1}})
Doesn't work either.
Am I missing something here?
Q1: If you update with permalink 'jaiho' instead of 'haha', it most certainly updates the email;
> db.posts.update({"permalink" : "jaiho"},{$set:{"comments.0.email":1}})
> db.posts.find()
..., "email" : 1 },...
Q2: Same goes for this include;
> db.posts.update({"permalink" : "jaiho"},{$inc:{"comments.0.num_likes":1}})
> db.posts.find()
..., "num_likes" : 1 },...
If you are trying to do it dynamically in Node JS following should work.
i = 0;
selector = {};
operator = {};
selector['comments.' + i + '.email'] = 1; // {'comments.0.num_likes' : 1}
operator['$inc'] = selector; // {'$inc' : {'comments.0.num_likes' : 1} }
db.posts.update({'permalink' : 'xyz'}, operator);

Resources