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
Related
I am finding a problem in group hashtags: Any Solution?
here is my query statement: any help.
def trending_tags(user)
#tags= HashTag.joins(:vibe_hash_tags).limit(10).where('hash_tags.created_at BETWEEN ? AND ?', 1.months.ago, Time.now).select("hash_tags.id, hash_tags.address as country, count(vibe_hash_tags.id) as vibes_hash_tags,COUNT(hash_tags.id)as hash_tag_count")
.group('COUNT(hash_tags.name as hash_tag_name)')
render :status => 200, :json => {:success => true, :result => { :trending_hash_tags => #tags}}
end
I would want to group it to this
{
"success": true,
"result": {
"trending_hash_tags": [
{
"id": 83,
"country": "TV",
"vibes_hash_tags": 2,
"hash_tag_name": "money",
"hash_tag_count": 2
},
{
"id": 84,
"country": "TV",
"vibes_hash_tags": 3,
"hash_tag_name": "grace",
"hash_tag_count": 3
}
]
}
}
I have been able to find a solution my question for grouping Hash Tags.
def trending_tags
#hash_tags= HashTag
.select('hash_tags.name AS hash_tag_name, COUNT(hash_tags
.id) AS hash_tag_count, hash_tags.address AS country, count(vibe_hash_tags.id) AS vibes_hash_tags')
.joins(:vibe_hash_tags)
.order('hash_tag_count DESC')
.group('hash_tags
.name, hash_tags.address').limit(10).where('hash_tags.created_at BETWEEN ? AND ?', 5.days.ago, Time.now)
render :status => 200, :json => {:success => true, :result => { :trending_hash_tags => #hash_tags}}
end
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)
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();
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()
}
})
Given the architecture below, is there a way that I can query the people collection based on the searchable columns and get back an object such as this?
organization with people: {
_id: "13efdsf31r23rwes"
name: "Name",
wp_slug: "/wordpress-slug/",
type: "Cemetary",
file_name: "something.csv",
columns: [
{ label: "Maiden Name", name: "maiden_name", hidden: false },
{ label: "First Name", name: "first_name", hidden: false },
{ label: "First Pet", name: "first_pet", hidden: true },
],
people: [
{
_id: "13okekdsf12313",
"organization_id": 13efdsf31r23rwes,
"maiden_name": "Gilomen",
"surname": "Black",
"first_name": "Maria",
"first_pet": "Bud the Dog",
"searchable_columns": {
"surname": ["Black", "Gilomen"],
"given": ["Maria"],
}
},
{
_id: "1231sdf1313ewfd",
"organization_id": 13efdsf31r23rwes,
"maiden_name": "Portland",
"surname": "Smith",
"first_name": "Rose",
"first_pet": "Max the Dog",
"searchable_columns": {
"surname": ["Smith", "Portland"],
"given": ["Rose"],
}
},
]
}
Current architecture:
organization document example: {
_id: "13efdsf31r23rwes"
name: "Name",
wp_slug: "/wordpress-slug/",
type: "Cemetary",
file_name: "something.csv",
columns: [
{ label: "Maiden Name", name: "maiden_name", hidden: false },
{ label: "First Name", name: "first_name", hidden: false },
{ label: "First Pet", name: "first_pet", hidden: true },
],
}
people collection: [
{
_id: "13okekdsf12313",
"organization_id": 13efdsf31r23rwes,
"maiden_name": "Gilomen",
"surname": "Black",
"first_name": "Maria",
"first_pet": "Bud the Dog",
"searchable_columns": {
"surname": ["Black", "Gilomen"],
"given": ["Maria"],
}
},
{
_id: "1231sdf1313ewfd",
"organization_id": 13efdsf31r23rwes,
"maiden_name": "Portland",
"surname": "Smith",
"first_name": "Rose",
"first_pet": "Max the Dog",
"searchable_columns": {
"surname": ["Smith", "Portland"],
"given": ["Rose"],
}
},
]
Unfortunately we have hit the 16M document limit when they are stored as a combined document, so I need to do this workaround. Is this possible to do with a straight MongoDB query, or is there no other way than to query twice? There are about 5 million records of people and 500+ organizations. Any help would be appreciated, especially if shown in MongoPHP format.
I was able to workaround the size limitation. Using the "desired structure", I split the intended document into multiple documents of a size under 16M. Using an aggregate query, I was able to combine all of the splits back together.
$documents_with_results = $db->organization_data->aggregate(array(
array( '$match' => $search_column_query),
array( '$unwind' => '$p' ),
array( '$match' => $search_column_query),
array( '$group' => array(
'_id' => '$_id',
'name' => array( '$first' => '$n' ),
'slug' => array( '$first' => '$ws' ),
'people' => array( '$push' => '$p' )
))
));