How to update the document on solr 6.0? - solr

I am using solr 6.0 version.
This is my data
{
"id" : "14",
"solrid" : "solr|school|14",
"name" : "test update solr 14",
"status" : "pending",
"state" : "Andhra Pradesh",
"board" : "CISCE",
"updated" : "2016-05-26T02:24:25Z",
"pincode" : "0"
}
I want to update the data on document as per id.
example i want to change the name
$doc = $update->createDocument();
$doc["id"] =$id;
$doc["name"]="school";
$update->addDocument($doc);
$update->addCommit();
$client->update($update);
This code is correct? Or i want to use other flow.
PHP Solarium code.

Yes, basically the flow is correct. You can always check the solarim examples gist at https://gist.github.com/basdenooijer/894286.
Alternatively you can do something like this:
$update = $client->createUpdate();
$document = $update->createDocument();
$document->setKey('id', $id);
foreach ($data as $field => $value) {
if ($field == 'id') {
continue;
}
$document->setField($field, $value, null, 'set');
}
$update->addDocument($document, true, 1500);
$result = $client->update($update);

Related

Removing a document from array of documents in mongodb using c#

I want to remove the highest value price document
{
"name" : "Ryan",
"price" : 6.5
}
from
db.books.find({_id:5}).pretty()
{
"_id" : 5,
"name" : "Big Book",
"authors" : [
{
"name" : "Ryan",
"price" : 5.5
},
{
"name" : "Ryan",
"price" : 6.5
}
]
}
using c# driver
I have the removable document in c# front end in a collection.
But my following update statement is not removing the document.
var filter = new BsonDocument("_id", x._id);
var docToRemove = new BsonDocument("name", x.name);docToRemove.Add("price", x.price);
var update = Builders<Book>.Update.Pull("books.authors", docToRemove);
var result = await col.FindOneAndUpdateAsync(filter, update);
Do you have required value in x.price?
I didn't test it for exactly your case, but try this:
var update = Builders<Book>.Update.PullFilter(p => p.authors,
f => f.price == x.price);
var result = await collection.FindOneAndUpdateAsync(p => p._id == 5, update);

Mongodb: Query a json-object nested in an array

I'm quite new to mongodb and there is one thing I can't solve right now:
Let's pretend, you have the following document (simplified):
{
'someKey': 'someValue',
'array' : [
{'name' : 'test1',
'value': 'value1'
},
{'name' : 'test2',
'value': 'value2'
}
]
}
Which query would return the json-object, in which the value equals 'value2'?
That means, i need this json-object:
{
'name' : 'test2',
'value': 'value2'
}
Of course I already tried a lot of possible queries, but none of them returned the right, e.g.
db.test.find({'array.value':'value2'})
db.test.find({'array.value':'value2'}, {'array.value':1})
db.test.find({'array.value':'value2'}, {'array.value':'value2'})
Can someone help and show me, what I'm doing wrong?
Thanks!
Using Positional operator
db.test.find(
{ "array.value": "value2" },
{ "array.$": 1, _id : 0 }
)
Output
{ "array" : [ { "name" : "test2", "value" : "value2" } ] }
Using aggregation
db.test.aggregate([
{ $unwind : "$array"},
{ $match : {"array.value" : "value2"}},
{ $project : { _id : 0, array : 1}}
])
output
{ "array" : { "name" : "test2", "value" : "value2" } }
Using Java Driver
MongoClient mongoClient = new MongoClient(new ServerAddress("localhost", 27017));
DB db = mongoClient.getDB("mydb");
DBCollection collection = db.getCollection("test");
DBObject unwind = new BasicDBObject("$unwind", "$array");
DBObject match = new BasicDBObject("$match", new BasicDBObject(
"array.value", "value2"));
DBObject project = new BasicDBObject("$project", new BasicDBObject(
"_id", 0).append("array", 1));
List<DBObject> pipeline = Arrays.asList(unwind, match, project);
AggregationOutput output = collection.aggregate(pipeline);
Iterable<DBObject> results = output.results();
for (DBObject result : results) {
System.out.println(result.get("array"));
}
output
{ "name" : "test2" , "value" : "value2"}
Try the $in operator like this:
db.test.find({"array.value" : { $in : ["value2"]}})
You can pass multiple find objects in element match for getting the exact answer.
db.test.find({'array':{$elemMatch:{value:"value2"}})
output: {'name' : 'test1','value': 'value1'}
Use $elemMatch and dot(.) to get your required output
db.getCollection('mobiledashboards').find({"_id": ObjectId("58c7da2adaa8d031ea699fff") },{ viewData: { $elemMatch : { "widgetData.widget.title" : "England" }}})

how to add a document to an array in mongodb using powershell code?

I would like to insert a document into an array within another document in Powershell.
e.g.
{
"1" : "A",
"2" : "B",
"3" : [
{
"a" : "AA",
"_id" : ObjectId("5333b74c9e99569836bca41f")
}
],
"_id" : ObjectId("5333b74c9e99569836bca41c")
}
this is my document in mongodb shell.
{
"1" : "A",
"2" : "B",
"3" : [ ]
"_id" : ObjectId("5333b74c9e99569836bca41c")
}
and this is my code in powershell:
$embededDocument = new-object Mongodb.Bson.BsonDocument
$embededDocument.Add("a", "AA")
$embededDocument.Add("B2", "2")
$embededDocument.Add("C2", "3")
$parentDocument["3"].Add($embededDocument)
this is not working as the array is empty after run the code, I tried using Add, Insert and Push with same result...
Any idea what I'm doing wrong?
thank you very much
Basically you are trying to do an update of the type that is shown for the $push operator, as shown in the documentation. So you need to construct the same sort of BSON document structure as shown there:
$query = [MongoDB.Driver.Builders.Query]::EQ("_id" :
[MongoDB.Bson.BsonObjectId]::Create("5333b74c9e99569836bca41c"))
$doc = new-object MongoDB.Bson.Document
$doc.Add("a", "AA")
$inner = new-object MongoDB.Bson.Document
$inner.Add( "3", $doc )
$update = new-object MongoDB.Bson.Document
$update.Add('$push', $inner)
$collection.update($query,$update)
This comes out the be the equivalent of this:
db.collection.update(
{ _id: ObjectId("5333b74c9e99569836bca41c") },
{
"$push": { "3": { "a": "AA" } }
}
)
MongoDB does not create ObjectId values automatically inside array elements in the way you have shown. If you want to create them then you need to add the fields and created ObjectId values yourself.
For any other reference, use the C# documentation which is essentially the API you are using with powershell.
If you use the push operation or such like that,you are supposed to call the save() after that or I don't think your document would be saved.
As a result, your array will always be null.

Echo "part" of array (decoded from JSON)

I'am quite new to JSON and more "advanced" arrays. Therefore I don't know what I should search for...
I have this "JSON array" (what do you call it?):
{
"id": "321123321",
"statuses": {
"data": [
{
"message": "testmessage",
"updated_time": "2012-12-25T16:33:29+0000",
"id": "123321123"
}
],
"paging": {
"previous": "1",
"next": "1"
}
}
}​
I want to create a variable from "message" that is called $message and a variable from "up_datedtime" that is called $updated.
To get id I simple:
$json_a=json_decode($string,true);
$id $json_a['id'];
And for statuses:
$json_a=json_decode($string,true);
$status = $json_a['id']['statuses'];
But when I try to get "message" I get " Cannot use string offset as an array in":
$message = $json_a['id']['statuses']['data']['message'];
How do I get $message from the array the proper way?
You can get like this
$message = $json_a['id']['statuses']['data'][0]['message'];
or you can get from loop
$dataArr = $json_a['id']['statuses']['data'];
foreach ($dataArr as $val) {
echo "message".$val['message'];
}

How do I change my data accessing way from data[key] to data->key in laravel collection?

I got a data structure like below that is created by laravel collection put() function.
Code:
$array= collect();
$array->put($id, "1");
$array->put($name, "test");
Result:
{
"id": "1",
"name": "test"
}
How can I get the value with this syntax $array->id instead of this $array['id']?
Check this answers, https://stackoverflow.com/a/56565951/641611
$arr = ['id' => 3];
$arrToObject = (object) $arr;
echo $arrToObject->id;

Resources