Laravel Mongodb Messenger relations data not filtering - mongodb

I have 3 collection instances with belongsToMany relations.
I want to filter data from all 3 collections but not sure how.
Collection Structure.
Payment:
{
"_id": "5ba3c8f7c2ab9b3bed20ae22",
"code": "GHFH",
"amount": "345.00"
}
User:
{
"_id": "5ba3c87cc2ab9b34be139382",
"name": "Jack",
"email": "Pd#gmail.com",
"payment_ids": [
"5ba3c8f7c2ab9b3bed20ae22"
]
}
Customer:
{
"_id": "23c34c87cc2ab9b34b3423423",
"name": "cfb",
"email": "Psfsdd#gmail.com",
"payment_ids": [
"5ba3c8f7c2ab9b3bed20ae22"
]
}
I have create below query.
$cruds = payment::with([
'user' => function ($query) {
$query->orWhere('name','like', "%'".$search."'%");
},
'customer' => function ($query) {
$query->orWhere('name','like', "%'".$search."'%");
}
])
->orWhere('code','like', "%'".$search."'%")
->orWhere('amount', 'like', "%'".$search."'%")
->offset($start)
->limit($limit)
->orderBy($order, $dir)
->get();
Please Suggest.
I have change in query.
$cruds = payment::with([
'user' => function ($query) {
$query->where('name','like', "%{$search}%");
},
'customer' => function ($query) {
$query->where('name','like', "%{$search}%");
}
])
->where('code','like', "%{$search}%")
->where('amount', 'like', "%{$search}%")
->offset($start)
->limit($limit)
->orderBy($order, $dir)
->get();

Related

Updating a nested array object in Mongo

I have a StudentClass collection that looks like this:
[
{
"_id": ObjectId("60923c997b4d3205009a981a"),
"attended": [
{
"in": "2021-05-05T06:35:05.226+00:00",
"out": "2021-05-05T06:45:05.226+00:00"
},
{
"in": "2021-05-05T06:47:05.226+00:00",
"out": "2021-05-05T06:55:05.226+00:00"
},
{
"in": "2021-05-05T06:56:05.226+00:00"
},
{
"in": "2021-03-03T07:10:00.628Z"
}
],
"studentId": ObjectId("608a42e8224c549ad9a9ab51"),
"active": true
},
{
"_id": ObjectId("6098f6f974af29682772fbe6"),
"attended": [
{
"in": "2021-05-05T06:35:05.226+00:00",
"out": "2021-05-05T06:55:05.226+00:00"
},
{
"in": "2021-05-05T06:59:05.226+00:00",
"out": "2021-05-05T07:20:05.226+00:00"
}
],
"studentId": ObjectId("608a42e8224c549ad9a9ab51"),
"active": true
},
{
"_id": ObjectId("6098f6f974af29682772fbe7"),
"attended": [],
"studentId": ObjectId("608a42e8224c549ad9a9ab51"),
"active": true
}]
For a give _id, I need to update the attended array as per the following conditions:
Need to find the last array element with a missing out key. That array element of the attended should get updated with the out key just like other array elements.
This is what I've tried:
const markStudentExitFromClass = (studentClassId, exitTime) => {
return new Promise((resolve, reject) => {
StudentClasses.updateOne(
{ _id: new mongoose.Types.ObjectId(studentClassId), "attended.out" : {$exists: false}},
{ $set: { "attended.$.out": new Date(exitTime) } },
function (err, updatedData) {
if (err) {
reject(err);
} else {
resolve(updatedData);
}
}
)
})
}
This is not updating any array element of attended array for the give _id. Just like $ positional operator finds the first array element, is there anything for the last array element?
What am I doing wrong?
Updated:
So, I got the update part working by changing the match clause of updateOne to:
const markStudentExitFromClass = (studentClassId, exitTime) => {
return new Promise((resolve, reject) => {
StudentClasses.updateOne(
{ _id: new mongoose.Types.ObjectId(studentClassId), "attended" :{"$elemMatch":{"out":{$exists: false}}} },
{ $set: { "attended.$.out": new Date(exitTime) } },
function (err, updatedData) {
if (err) {
reject(err);
} else {
resolve(updatedData);
}
}
)
})
}
But, I still cant figure out how to get the last matching element of the attended array instead of the first. Any pointers?

Mongoose nested object not updating 'cannot create field "foo" in element'

I have a similar issue to this question.
I'm trying to create a new field using "findAndUpdate". I've tried all the methods, $set, $push, $addSet... none of them seem to be working and I keep getting the same error.
Here's the code:
router.post('/accept', auth, async (req, res) => {
const useremail = user.email
const originalEvent = await Event.findOneAndUpdate({eventId: 61469041, isOrganizer: true, "attendees.email": useremail},
{"$push":{"attendees.status" : "accepted"}},
{new: true})
res.status(200).json({originalEvent, event})
}
catch (e) {
res.status(400).json({ msg: e.message, success: false });
}
});
Here's the error code:
"Cannot create field 'status' in element {attendees: [ { _id: ObjectId('5f80a02a82dceb2810e0aa66'), email: "bob#gmail.com", name: "Bob" } ]}"
Here's the object I'm trying to update:
{
"organizer": {
"email": "alex#gmail.com",
"name": "Alex"
},
"_id": "5f80a02a82dceb2810e0aa65",
"title": "Go to the beach",
"eventId": 61469041,
"isOrganizer": true,
"user": "5f05f23417ca6ab69ccc4cf2",
"attendees": [
{
"_id": "5f80a02a82dceb2810e0aa66",
"email": "bob#gmail.com",
"name": "Bob"
}
],
"__v": 0,
}
Expected outcome:
{
"organizer": {
"email": "alex#gmail.com",
"name": "Alex"
},
"_id": "5f80a02a82dceb2810e0aa65",
"title": "Go to the beach",
"eventId": 61469041,
"isOrganizer": true,
"user": "5f05f23417ca6ab69ccc4cf2",
"attendees": [
{
"_id": "5f80a02a82dceb2810e0aa66",
"email": "bob#gmail.com",
"name": "Bob",
"status": "accepted"
}
],
"__v": 0,
}
SOLVED with this:
const originalEvent = await Event.findOneAndUpdate({eventId: eventId, "isOrganizer": true,
"attendees": {$elemMatch: {email: useremail}}
},
{ $set: { "attendees.$.status": "accepted"} }
)
res.status(200).json(originalEvent)
}
Referencing attendees.status doesn't make sense because in your schema attendees is not an object (with fields such as status) but an array. But you can do it differently. If you have the index of the attendee you want to mutate, you can do { $set: { "attendees.0.status": "accepted" } }, where 0 is the index in the array.
Also, with regards to the first half of your question, the error you're seeing is because $push works on arrays. So in order for your operation to work, you'd have to first initialize such an object {attendees: { status: [] } }.
If the field is not an array, the operation will fail. (docs)

Setting extraFields in rest api yii2

public function fields()
{
return [
'field' => 'field',
];
}
public function extraFields()
{
return [
'users',
];
}
return:
{ "field": "field", "users": { "id": 1, "name": "user" } }
how to exclude id?
public function extraFields()
{
return [
'users' => function($model){
return [
'name' => $model->users->name,
];
}
];
}
return:
{ "field": "field", "users": { "name": null } }
how to fill in the name field correctly or how to customize field output filtering?
Option can be overwrite field() method in User model:
public function fields()
{
$fields = parent::fields();
if ($something) {
unset($fields['id']);
}
return $fields;
}

Finding multiple docs using same id not working, using meteor + react and mongoDB

How do I get the email address of the students in the same class_id, take it as there are more then 2 students in different class in the DB as well?
I have this but it return empty array []
Meteor.users.find({"course_learn_list.$.class_id": {$in: [classId]}},
{field: {"emails.address": 1}}
).fetch()
Collections
{
"_id": "LMZiLKs2MRhZiiwoS",
"course_learn_list": [
{
"course_id": "M8EiKfxAAzy25WmFH",
"class_id": "jePhNgEuXLM3ZCt98"
},
{
"course_id": "5hbwrfbfxAAzy2nrg",
"class_id": "dfbfnEuXLM3fngndn"
}
],
"emails": [
{
"address": "student1#gmail.com",
"verified": false
}
]
},
{
"_id": "JgfdLKs2MRhZJgfNgk",
"course_learn_list": [
{
"course_id": "M8EiKfxAAzy25WmFH",
"class_id": "jePhNgEuXLM3ZCt98"
},
{
"course_id": "5hbwrfbfxAAzy2nrg",
"class_id": "dfbfnEuXLM3fngndn"
}
],
"emails": [
{
"address": "student2#gmail.com",
"verified": false
}
]
}
I think you want:
Meteor.users.find({ "course_learn_list.class_id": classId },
{ "course_learn_list.$": 1, "emails.address": 1 }).fetch()
This should find the first instance in each course_learn_list array where the classId is your classId.
In this case you probably don't need to use a projection to get the right answer. Here's an example of extracting the verified email addresses using only the . operator in the selector:
const ids = ['jePhNgEuXLM3ZCt98', 'some-other-id'];
const emails =
Meteor.users.find({ 'course_learn_list.class_id': { $in: ids } })
.fetch()
.map(user => _.findWhere(user.emails, { verified: true }).address);
This works for me!
Meteor.publish("getMyClassStudents", function(classId) {
console.log("Publish getMyClassStudents")
var self = this
if (self.userId) {
var data = Meteor.users.find({
"course_learn_list.class_id": classId
}, {
"fields": {
"emails.address": 1
}
})
return data
}
else {
return self.ready()
}
})

MongoDB $addToSet not writing to database

I am not sure why but I am having a really hard time getting the $addToSet working for my sub array items.
This is what it should look like:
{
"items": [
{
"id": "510bca8138fc5d6e38000000",
"quantity": "1"
},
{
"id": "51011a8138fc5d6348000000",
"quantity": "1"
}
],
"session": "1359948849.291898629576",
"status": "cart"
}
However, it seems to only allow the first:
{
"items": [
{
"id": "510bca8138fc5d6e38000000",
"quantity": "1"
}
],
"session": "1359948849.291898629576",
"status": "cart"
}
and it just won't insert another sub array.
My code:
$document = $collection->findOne(array('session' => $_SESSION["redi-Shop"]));
//print_r($document);
if (null !== $document) {
$collection->update(
array('session' => $_SESSION["redi-Shop"]),
array(
'$addToSet' => array(
'items' => $_POST['item']
),
));
print_r($_POST['item']);
}
else
{
$collection->insert(
array('session' => $_SESSION["redi-Shop"],
'status' => "cart",
'items' => $_POST['item'])
);
}
I changed the $addToSet to $Push and it works fine