Mongoid find_and_modify deprecated - mongoid

I'm currently updating a Rails app to Mongoid 5. I've having trouble updating some code that uses a deprecated method (find_and_modify). Any help would be appreciated.
In Mongoid 4, I have this method to find and upsert:
LineItem.where({
date: Date.today,
location: "Location",
department: "Department"
}).find_and_modify({
"$set" => {
hours: 8,
updated_at: Time.current
},
"$setOnInsert" => {
account_id: ObjectId("5739f4534f4e48b2aa00091c"),
date: Date.today,
location: "Location",
department: "Department",
created_at: Time.current
}
}, upsert: true)
What is the equivalent using Mongoid 5?
Thanks.

From this changelog:
find_and_modify has been removed and replaced with 3 options: find_one_and_update,
find_one_and_delete and find_one_and_replace.

Related

mongodb update nested array from a array

I have a array in mongodb document.
{
_id: 1,
jobs:[
{
_id:1,
time: "08:00",
status: "pending",
user: 'user1'
},
{
_id:2,
time: "09:00",
status: "pending",
user: 'user1'
},
{
_id:3,
time: "07:30",
status: "done",
user: 'user2'
}
]
}
now I have a updated jobs array like this.
jobs:[
{
_id:1,
time: "10:00",
status: "done"
},
{
_id:2,
time: "11:00",
status: "done"
}
]
updated document should like this
{
_id: 1,
jobs:[
{
_id:1,
time: "10:00", // updated
status: "done" // updated
user: 'user1'
},
{
_id:2,
time: "11:00", // updated
status: "done", // updated
user: "user1"
},
{
_id:3,
time: "07:30",
status: "done",
user: 'user2'
}
]
}
I tried using update and $set and no luck so far
how do I update the only the values in the updated array in to the mongodb document? thanks in advance
One option is using an update with a pipeline:
Add the new data into the document as newData
Using a $map to loop over the jobs items, for each item merge it with the matching item in newData.
EDIT (consider partial match):
db.collection.update(
{_id: 1},
[{$addFields: {
newData: [
{_id: 1, time: "10:00", status: "done"},
{_id: 2, time: "11:00", status: "done"}
]
}
},
{$project: {
jobs: {$map: {
input: "$jobs",
in: {$mergeObjects: [
"$$this",
{$cond: [
{$gte: [{$indexOfArray: ["$newData._id", "$$this._id"]}, 0]},
{$arrayElemAt: ["$newData", {$indexOfArray: ["$newData._id", "$$this._id"]}]},
]}
]}
]}
}}
}}
])
See how it works on the playground example

How to add and update object into array of objects in PostgreSql?

I am learning postgresql. I have this type of data that i want to add in my database.
{
id: (Math.random().toString(36)+'00000000000000000').slice(2, 7),
title: "This is a sample todo",
status: 'pending',
created_at: new Date(),
subTask: [
{
text: "This is a sampe subtask",
isDone: false,
},
{
text: "This is a sample subtask",
isDone: false,
}
]
}
My question is how can i update and add objects in subtasks array using postgresql queries.
I have so far tried this one
update
todos
set
subtasks = '[{"title": "Task No 2", "status": "pending"}]'
where
id = 6;
But it is only replacing the existing object present not adding it. I have also searched on the google and elsewhere but so far don't find anything.
Thank you

MongoDB check if key value exists and update only some fields

Let's say I have a MongoDB collection "people" that has the form
[
{
_id: [OBJECT_ID_1],
name: "Paul",
hobby: "rowing",
fixed: 1
},
{
_id: [OBJECT_ID_2],
name: "Selena",
hobby: "drawing",
fixed: 2
},
{
_id: [OBJECT_ID_3],
name: "Emily",
hobby: "jogging",
fixed: 3
}
]
And new data to be inserted of the form
var data = [
{
name: "Paul", // name exists, so keep "fixed" at 1
hobby: "archery",
fixed: 4
},
{
name: "Peter",
hobby: "knitting",
fixed: 5
}
]
I would like to insert/update the collection with the new data. However, if a document with the same "name" already exists, I do not want to update "fixed". The result after inserting the above data should be
[
{
_id: [OBJECT_ID_1],
name: "Paul",
hobby: "archery", // updated
fixed: 1 // not updated, because name existed
},
{
_id: [OBJECT_ID_2],
name: "Selena",
hobby: "drawing",
fixed: 2
},
{
_id: [OBJECT_ID_3],
name: "Emily",
hobby: "jogging",
fixed: 3
},
{ // newly inserted document
_id: [OBJECT_ID_4],
name: "Peter",
hobby: "knitting",
fixed: 5
}
]
The data includes a large number of documents, so I would like to achieve this in one query if possible. What would be the best way to accomplish this? Many thanks!
bulkWrite with updateOne's with $upsert:true seems to work best for you...
bulkWrite not perform a Find operation.
Its (in my case) is necessary to control if it will create a another document.
In my case I just use find check before insert/create method.
if (collection.find({"descr":descr}).limit(1).length === 1) {
//...create method
console.log('Exists')
}

Insert data to Atlas MongoDB error appearing: Insert not permitted while document contains errors

I'm trying to insert data to a collection I created in Atlas MongoDB. The data is following:
[
{ id: 1, performer: 'John Doe', genre: 'Rock', price: 25, day: 1, image: '/img/uploads/1fsd324fsdg.jpg' },
{ id: 2, performer: 'Rebekah Parker', genre: 'R&B', price: 25, day: 1, image: '/img/uploads/2f342s4fsdg.jpg' },
{ id: 3, performer: 'Maybell Haley', genre: 'Pop', price: 40, day: 1, image: '/img/uploads/hdfh42sd213.jpg' }
]
`I get the error : "Insert not permitted while document contains errors."
What am I doing wrong? Please advise.
may be quote problem ...
use double quote to key and property
"id" : 1, "performer : "John Doe" .. ~
This is reslved now, formatting was the problem.
I do the following:
Modify title to "title" and mongo compass works.
Example:
[
{ "id": "1", "performer": "John Doe", "genre": "Rock", "price": "25", "day": "1", "image": "/img/uploads/1fsd324fsdg.jpg" },
]
Reference document: https://docs.mongodb.com/compass/current/documents/insert
It's better that you use MongoDB Compass and connect to it with a connection string:
click on the connection
click on the connection using mongodb compass
then get the compass downloaded from MongoDB according to required OS
but use connection string of connection to MongoDB application
Once you connect to your Compass, you can use import data and then browse your file to use.

getting array values ruby

I have an array which is result of a query.
r = [
#<Reservation id: 27, schedule_id: 1, name: "test user", gender: "no_answer",
reservation_num: 1, zip: "", prefecture: "", address: "", street: "",
tel: "", note: "", satisfied: true, canceled: false, seq: 27,
created_at: "2019-08-28 06:04:30", updated_at: "2019-08-28 06:04:30",
created_by: 2, updated_by: 2, from_partner: false, no_counting: false,
reservation_time: nil, one_day_payment: nil, payment_id: 123456>
]
I want to get the payment_id but don't understand how should I write.
If you have number of records in array, you can get payment_id in form of array as below,
r.map(&:payment_id)
Looks like you should be able to select the first reservation from that array and then call on the payment_id. Like:
r.first.payment_id
To get all payment_id's you can do
r.map(&:payment_id)
You can also use the collect method
r.collect(&:name)
You can pluck payment ids (from the required set of data) with the following :
Reservation.pluck(:payment_id)

Resources